home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / cpphtp2 / cpphtp2.jar / chpt_06.gml < prev    next >
Text File  |  1998-03-03  |  137KB  |  3,461 lines

  1. <html>
  2. <chapter>
  3. <section type=Popup name=Quotes title="Quotes">
  4. <page>
  5. <i>My object all sublime       
  6. I shall achieve in time.</i> <br>
  7. W. S. Gilbert<br>
  8. <br>
  9.  
  10. </page>
  11. <page>
  12. <i>Is it a world to hide 
  13. virtues in?</i>  <br>
  14. William Shakespeare, 
  15. Twelfth Night<br>
  16. <br>
  17.  
  18. </page>
  19. <page>
  20. <i>Your public servants 
  21. serve you right.</i> <br>
  22. Adlai Stevenson<br>
  23. <br>
  24.  
  25. </page>
  26. <page>
  27. <i>Private faces in public 
  28. places                              
  29. Are wiser and nicer             
  30. Than public faces in 
  31. private places.</i> <br>
  32. W. H. Auden<br>
  33. <br>
  34.  
  35. </page>
  36. </section>
  37. <section type=Popup name=Answers title="Answers">
  38. <page pagename="Answer 6.1">
  39. <b>Answer 6.1</b><br>
  40. a) <b>struct</b>.<br>
  41. b) dot (<b>.</b>), arrow (<b>-></b>).<br>
  42. c) <b>private</b>.<br>
  43. d) constructor.<br>
  44. e) <b>private</b>.<br>
  45. f) <i>set</i>.<br>
  46. g) Default memberwise copy (performed by the assignment operator). <br>
  47. h) <b>public</b>, <b>private</b>.<br>
  48. i) <i>get</i>.<br>
  49. j) interface.<br>
  50. <foreign  name="exercises" url="^Exercises::c:s0p0">
  51.  
  52. </page>
  53. <page pagename="Answer 6.1">
  54. k) encapsulated.<br>
  55. l) <b>class</b>, <b>struct</b>.<br>
  56. m) <b>public</b>. <br>
  57. <foreign  name="exercises" url="^Exercises::c:s0p0">
  58. <br>
  59.  
  60. </page>
  61. <page pagename="Answer 6.2">
  62. <b>Answer 6.2</b><br>
  63. a)  Error: Destructors are not allowed to return values or take arguments.<br>
  64.      Correction: Remove the return type <b>void</b> and the parameter <b>int</b> from the 
  65. declaration.<br>
  66. b)  Error: Members cannot be explicitly initialized in the class definition.<br>
  67.      Correction: Remove the explicit initialization from the class definition and 
  68. initialize the data members in a constructor.<br>
  69. c)  Error: Constructors are not allowed to return values.<br>
  70.      Correction: Remove the return type <b>int</b> from the declaration.<br>
  71. <foreign  name="exercises" url="^Exercises::c:s0p3">
  72. <br>
  73.  
  74. </page>
  75. <page pagename="Answers 6.5 ">
  76. <b>Answers 6.5 </b><br>
  77. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  78. the file cpphtp2/answers/P6_05.zip to your hard drive and unzip the program 
  79. code.<br>
  80. <foreign  name="exercises" url="^Exercises::c:s0p7">
  81. <br>
  82.  
  83. </page>
  84. <page pagename="Answers 6.7 ">
  85. <b>Answers 6.7 </b><br>
  86. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  87. the file cpphtp2/answers/P6_07.zip to your hard drive and unzip the program 
  88. code.<br>
  89. <foreign  name="exercises" url="^Exercises::c:s0p10">
  90.  
  91. </page>
  92. <page pagename="Answers 6.8 ">
  93. <b>Answers 6.8 </b><br>
  94. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  95. the file cpphtp2/answers/P6_08.zip to your hard drive and unzip the program 
  96. code.<br>
  97. <foreign  name="exercises" url="^Exercises::c:s0p12">
  98. <br>
  99.  
  100. </page>
  101. <page pagename="Answers 6.12 ">
  102. <b>Answers 6.12 </b><br>
  103. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  104. the file cpphtp2/answers/P6_12.zip to your hard drive and unzip the program 
  105. code.<br>
  106. <foreign  name="exercises" url="^Exercises::c:s0p16">
  107.  
  108. </page>
  109. <page pagename="Answers 6.16 ">
  110. <b>Answers 6.16 </b><br>
  111. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  112. the file cpphtp2/answers/P6_16.zip to your hard drive and unzip the program 
  113. code.<br>
  114. <foreign  name="exercises" url="^Exercises::c:s0p20">
  115.  
  116. </page>
  117. </section>
  118. <section type=Body name=Default title="6 Classes and Data Abstraction">
  119. <page>
  120. <font size=18 bold>6 Classes and Data Abstraction</font><hr>
  121. <a href="#s1p0">6.1<spacer width=20 height=1>Introduction</a>  <br>
  122. <a href="#s2p0">6.2<spacer width=20 height=1>Structure Definitions</a>  <br>
  123. <a href="#s3p0">6.3<spacer width=20 height=1>Accessing Members of Structures</a>  <br>
  124. <a href="#s4p0">6.4<spacer width=20 height=1>Implementing a User-Defined Type Time With a 
  125. Struct</a>  <br>
  126. <a href="#s5p0">6.5<spacer width=20 height=1>Implementing a Time Abstract Data Type with a 
  127. Class</a>  <br>
  128. <a href="#s6p0">6.6<spacer width=20 height=1>Class Scope and Accessing Class Members</a>  <br>
  129. <a href="#s7p0">6.7<spacer width=20 height=1>Separating Interface from Implementation</a>  <br>
  130. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  131. <foreign  name="quotes" url="^Quotes::c:s0p0">
  132.  
  133. </page>
  134. <page>
  135. <a href="#s8p0">6.8<spacer width=20 height=1>Controlling Access to Members</a>  <br>
  136. <a href="#s9p0">6.9<spacer width=20 height=1>Access Functions and Utility Functions</a>  <br>
  137. <a href="#s10p0">6.10<spacer width=20 height=1>Initializing Class Objects: Constructors</a>  <br>
  138. <a href="#s11p0">6.11<spacer width=20 height=1>Using Default Arguments with Constructors</a>  <br>
  139. <a href="#s12p0">6.12<spacer width=20 height=1> Using Destructors</a>  <br>
  140. <a href="#s13p0">6.13<spacer width=20 height=1>When Constructors and Destructors are Called</a>  <br>
  141. <a href="#s14p0">6.14<spacer width=20 height=1>Using Data Members and Member Functions</a>  <br>
  142. <a href="#s15p0">6.15<spacer width=20 height=1>A Subtle Trap: Returning a Reference to a Private 
  143. Data Member</a>  <br>
  144. <a href="#s16p0">6.16<spacer width=20 height=1>Assignment by Default Memberwise Copy</a>  <br>
  145. <a href="#s17p0">6.17<spacer width=20 height=1>Software Reusability</a>  <br>
  146. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  147. <foreign  name="quotes" url="^Quotes::c:s0p0">
  148.  
  149. </page>
  150. <page>
  151. <a href="#s18p0">6.18<spacer width=20 height=1>Thinking About Objects: Programming the Classes 
  152. for the Elevator Simulator</a>  <br>
  153. <a href="#s19p0">6.19<spacer width=20 height=1>Elevator Laboratory Assignment 5</a>  <br>
  154. <a href="#s20p0">6.20<spacer width=20 height=1>Summary</a>  <br>
  155. <a href="^Terminology::c:s0p0">Terminology</a>  <br>
  156. <a href="^Illustration::c:s0p0">Figures</a>  <br>
  157. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  158. <foreign  name="quotes" url="^Quotes::c:s0p0">
  159.  
  160. </page>
  161. </section>
  162. <section type=Body name=Default title="6.1 Introduction">
  163. <page>
  164. <font size=18 bold>6.1 Introduction</font><hr>
  165. Now we begin our introduction to object orientation in 
  166. C++. Why have we deferred object-oriented 
  167. programming in C++ until Chapter 6? The answer is 
  168. that the objects we will build will be composed in part 
  169. of structured program pieces, so we needed to establish 
  170. a basis in structured programming first. <br>
  171. <spacer width=16 height=1>Through our "Thinking About Objects" sections at the 
  172. ends of Chapters 1 through 5, we have introduced the 
  173. basic concepts (i.e., "object think") and terminology 
  174. (i.e., "object speak") of object-oriented programming in 
  175. C++. In these special sections, we also discussed the <br>
  176.  
  177. </page>
  178. <page>
  179. techniques of <i>object-oriented design (OOD):</i> We 
  180. analyzed a typical problem statement that required a 
  181. system (an elevator simulator) to be built, determined 
  182. what classes were needed to implement the systems, 
  183. determined what attributes objects of these classes 
  184. needed to have, determined what behaviors objects of 
  185. these classes needed to exhibit, and specified how the 
  186. objects needed to interact with one another to 
  187. accomplish the overall goals of the system. <br>
  188. <spacer width=16 height=1>Let us briefly review some key concepts and 
  189. terminology of object orientation. OOP <i>encapsulates</i> 
  190. data (attributes) and functions (behavior) into packages 
  191. called <i>classes</i>; the data and functions of a class are <br>
  192.  
  193. </page>
  194. <page>
  195. intimately tied together. A class is like a blueprint. Out 
  196. of a blueprint, a builder can build a house. Out of a 
  197. class, a programmer can create an object. One blueprint 
  198. can be reused many times to make many houses. One 
  199. class can be reused many times to make many objects 
  200. of the same class. Classes have the property of 
  201. <i>information hiding</i>. This means that although class 
  202. objects may know how to communicate with one 
  203. another across well-defined <i>interfaces</i>, classes normally 
  204. are not allowed to know how other classes are 
  205. implemented--implementation details are hidden 
  206. within the classes themselves. Surely it is possible to 
  207. drive a car effectively without knowing the details of <br>
  208.  
  209. </page>
  210. <page>
  211. how engines, transmissions and exhaust systems work 
  212. internally. We will see why information hiding is so 
  213. crucial to good software engineering. <br>
  214. <spacer width=16 height=1>In C and other <i>procedural programming languages</i>, 
  215. programming tends to be <i>action-oriented</i>, whereas 
  216. ideally in C++ programming is <i>object-oriented</i>. In C, 
  217. the unit of programming is the <i>function</i>. In C++, the 
  218. unit of programming is the <i>class</i> from which objects are 
  219. eventually <i>instantiated</i> (i.e., created). <br>
  220. <spacer width=16 height=1>C programmers concentrate on writing functions. 
  221. Groups of actions that perform some task are formed 
  222. into functions, and functions are grouped to form 
  223. programs. Data is certainly important in C, but the view <br>
  224.  
  225. </page>
  226. <page>
  227. is that data exists primarily in support of the actions that 
  228. functions perform. The <i>verbs</i> in a system specification 
  229. help the C programmer determine the set of functions 
  230. that will work together to implement the system. <br>
  231. <spacer width=16 height=1>C++ programmers concentrate on creating their own 
  232. <i>user-defined types</i> called <i>classes</i>. Classes are also 
  233. referred to as <i>programmer-defined types</i>. Each class 
  234. contains data as well as the set of functions that 
  235. manipulate the data. The data components of a class are 
  236. called <i>data members</i>. The function components of a 
  237. class are called <i>member functions</i> (or <i>methods</i> in other 
  238. object-oriented languages). Just as an instance of a 
  239. built-in type such as int is called a <i>variable</i>, an <br>
  240.  
  241. </page>
  242. <page>
  243. instance of a user-defined type (i.e., a class) is called an 
  244. <i>object</i>. [In the C++ community, the terms variable and 
  245. object are often used interchangeably.] The focus of 
  246. attention in C++ is on classes rather than functions. The 
  247. <i>nouns</i> in a system specification help the C++ 
  248. programmer determine the set of classes that will be 
  249. used to create the objects that will work together to 
  250. implement the system. <br>
  251. <spacer width=16 height=1>Classes in C++ are a natural evolution of the C notion 
  252. of <b>struct</b>. Before proceeding with the specifics of 
  253. developing classes in C++, we discuss structures, and 
  254. we build a user-defined type based on a structure. The <br>
  255.  
  256. </page>
  257. <page>
  258. weaknesses we expose in this approach will help 
  259. motivate the notion of a class.<br>
  260.  
  261. </page>
  262. <page>
  263. <b>Select the true statement(s). </b><br>
  264. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  265. Objects normally are not allowed to know how other objects are implemented.  <br>
  266. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. The focus of attention is creating objects.">
  267. The focus of attention in C++ is creating functions.   <br>
  268. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  269. Classes are also referred to as programmer-defined types. <br>
  270. <component type=button name=b label="Check Your Answer" width=125 height=24>
  271.  
  272. </page>
  273. </section>
  274. <section type=Body name=Default title="6.2 Structure Definitions">
  275. <page>
  276. <font size=18 bold>6.2 Structure Definitions</font><hr>
  277. Structures are aggregate data types built using elements 
  278. of other types. Consider the following structure 
  279. definition:<br>
  280. <font size=2><br></font><font size=11><pre>
  281. struct Time {<p>
  282.    int hour;     // 0-23<p>
  283.    int minute;   // 0-59<p>
  284.    int second;   // 0-59<p>
  285. };<p>
  286. </pre></font>
  287. The keyword <b>struct</b> introduces the structure definition. 
  288. The identifier Time is the <i>structure tag</i> that names the 
  289. structure definition and is used to declare variables of 
  290. the <i>structure type</i>. In this example, the new type name <br>
  291.  
  292. </page>
  293. <page>
  294. is Time. The names declared in the braces of the 
  295. structure definition are the structure's <i>members</i>. 
  296. Members of the same structure must have unique 
  297. names, but two different structures may contain 
  298. members of the same name without conflict. Each 
  299. structure definition must end with a semicolon. The 
  300. preceding explanation is valid for classes also as we 
  301. will soon see; structures and classes are quite similar in 
  302. C++.<br>
  303. <spacer width=16 height=1>The definition of Time contains three members of type 
  304. <b>int--hour</b>, <b>minute</b>, and <b>second</b>. Structure members 
  305. can be any type, and one structure can contain members 
  306. of many different types. A structure cannot, however, <br>
  307.  
  308. </page>
  309. <page>
  310. contain an instance of itself. For example, a member of 
  311. type <b>Time</b> cannot be declared in the structure definition 
  312. for <b>Time</b>. A pointer to another <b>Time</b> structure, 
  313. however, can be included. A structure containing a 
  314. member that is a pointer to the same structure type is 
  315. referred to as a <i>self-referential structure</i>. Self-
  316. referential structures are useful for forming linked data 
  317. structures such as linked lists, queues, stacks and trees 
  318. as we will see in Chapter 15. <br>
  319. <spacer width=16 height=1>The preceding structure definition does not reserve any 
  320. space in memory; rather, the definition creates a new 
  321. data type that is used to declare variables. Structure <br>
  322.  
  323. </page>
  324. <page>
  325. variables are declared like variables of other types. The 
  326. declaration <br>
  327. <font size=2><br></font><font size=11><pre>
  328. Time timeObject, timeArray[ 10 ], *timePtr<p>
  329.      &timeRef = timeObject;<p>
  330. </pre></font>
  331. declares <b>timeObject</b> to be a variable of type <b>Time</b>, 
  332. <b>timeArray</b> to be an array with 10 elements of type 
  333. <b>Time</b>, <b>timePtr</b> to be a pointer to a <b>Time</b> object and 
  334. <b>timeRef</b> to be a reference to a <b>Time</b> object that is 
  335. initialized with <b>timeObject</b>.<br>
  336.  
  337. </page>
  338. <page>
  339. <b>Select the true statement(s). </b><br>
  340. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  341. Structures are created using the struct keyword.  <br>
  342. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Such variables are data members of the structure. ">
  343. Variable names declared in the braces ({}) of a structure definition are global variables. <br>
  344. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  345. Each structure definition must end with a ;. <br>
  346. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  347. A structure that contains a pointer member of the same structure type is called a self-referential structure. <br>
  348. <component type=button name=b label="Check Your Answer" width=125 height=24>
  349.  
  350. </page>
  351. </section>
  352. <section type=Body name=Default title="6.3 Accessing Members of Structures">
  353. <page>
  354. <font size=18 bold>6.3 Accessing Members of Structures</font><hr>
  355. Members of a structure (or of a class) are accessed 
  356. using the<i> member access operators</i>--the <i>dot operator</i> 
  357. (<b>.</b>) and the <i>arrow operator</i> (<b>-></b>). The dot operator 
  358. accesses a structure or class member via the variable 
  359. name for the object or via a reference to the object. For 
  360. example, to print member <b>hour</b> of structure 
  361. <b>timeObject</b> use the statement <br>
  362. <font size=2><br></font><font size=11><pre>
  363. cout << timeObject.hour;<p>
  364. </pre></font>
  365. To print member <b>hour</b> of the structure referenced by 
  366. <b>timeRef</b> use the statement<br>
  367. <font size=2><br></font><font size=11><pre>
  368. cout << timeRef.hour;<p>
  369. </pre></font>
  370.  
  371. </page>
  372. <page>
  373. The arrow operator--consisting of a minus sign (<b>-</b>) and 
  374. a greater than sign (<b>></b>) with no intervening spaces--
  375. accesses a structure member or class member via a 
  376. pointer to the object. Assume that the pointer <b>timePtr</b> 
  377. has been declared to point to a <b>Time</b> object, and that the 
  378. address of structure <b>timeObject</b> has been assigned to 
  379. <b>timePtr</b>. To print member <b>hour</b> of structure 
  380. <b>timeObject</b> with pointer <b>timePtr</b>, use the statements<br>
  381. <font size=2><br></font><font size=11><pre>
  382. timePtr = &timeObject;<p>
  383. cout << timePtr->hour;<p>
  384. </pre></font>
  385. The expression <b>timePtr->hour</b> is equivalent to 
  386. <b>(*timePtr).hour</b> which dereferences the pointer and 
  387. accesses the member hour using the dot operator.  <a href="^Errors::c:s0p0"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>The <br>
  388.  
  389. </page>
  390. <page>
  391. parentheses are needed here because the dot operator (<b>.</b>) 
  392. has a higher precedence than the pointer dereferencing 
  393. operator (<b>*</b>). The arrow operator and dot operator, along 
  394. with parentheses and brackets (<b>[]</b>), have the second 
  395. highest operator precedence (after the scope resolution 
  396. operator introduced in Chapter 3) and associate from 
  397. left to right.<br>
  398.  
  399. </page>
  400. <page>
  401. <b>Select the true statement(s). </b><br>
  402. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. The operator associates from left to right.">
  403. Operator -> associates from right to left.   <br>
  404. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  405. The operators -> and . are used to access structure members.  <br>
  406. <component type=button name=b label="Check Your Answer" width=125 height=24>
  407.  
  408. </page>
  409. </section>
  410. <section type=Body name=Default title="6.4 Implementing a User-Defined Type Time With a Struct">
  411. <page>
  412. <font size=18 bold>6.4 Implementing a User-Defined Type 
  413. Time With a Struct</font><hr>
  414. <a href="^Code::c:s0p0"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 6.1</a> creates the user-defined structure type <b>Time</b> 
  415. with three integer members: <b>hour</b>, <b>minute</b>, and 
  416. second. The program defines a single <b>Time</b> structure 
  417. called <b>dinnerTime</b> and uses the dot operator to 
  418. initialize the structure members with the values <b>18</b> for 
  419. <b>hour</b>, <b>30</b> for <b>minute</b>, and <b>0</b> for <b>second</b>. The program 
  420. then prints the time in military format (also called 
  421. "universal format") and standard format. Note that the 
  422. print functions receive references to constant <b>Time</b> 
  423. structures. This causes <b>Time</b> structures to be passed to <br>
  424.  
  425. </page>
  426. <page>
  427. the print functions by reference-- <a href="^Perform::c:s0p0"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>thus eliminating the 
  428. copying overhead associated with passing structures to 
  429. functions by value-- <a href="^Engineer::c:s0p0"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>and the use of <b>const</b> prevents the 
  430. <b>Time</b> structure from being modified by the print 
  431. functions. In Chapter 7, we discuss <b>const</b> objects and 
  432. <b>const</b> member functions.<br>
  433. There are drawbacks to creating new data types with 
  434. structures in this manner. Since initialization is not 
  435. specifically required, it is possible to have uninitialized 
  436. data and the consequent problems. Even if the data is 
  437. initialized, it may not be initialized correctly. Invalid 
  438. values can be assigned to the members of a structure (as 
  439. we did in  <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.1</a>) because the program has direct <br>
  440.  
  441. </page>
  442. <page>
  443. access to the data. In lines 30 and 31, the program was 
  444. easily able to assign bad values to the <b>hour</b> and <b>minute</b> 
  445. members of the <b>Time</b> object <b>dinnerTime</b>. If the 
  446. implementation of the <b>struct</b> is changed (e.g., the time 
  447. could be represented as the number of seconds since 
  448. midnight), all programs that use the <b>struct</b> must be 
  449. changed. <a href="^Engineer::c:s0p1"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>This is because the programmer directly 
  450. manipulates the data type. There is no "interface" to it 
  451. to ensure that the programmer uses the data type 
  452. correctly and to ensure that the data remains in a 
  453. consistent state. <br>
  454. <spacer width=16 height=1>There are other problems associated with C-style 
  455. structures. In C, structures cannot be printed as a unit; <br>
  456.  
  457. </page>
  458. <page>
  459. rather, their members must be printed and formatted 
  460. one at a time. A function could be written to print the 
  461. members of a structure in some appropriate format. 
  462. Chapter 8, "Operator Overloading," illustrates how to 
  463. overload the <b><<</b> operator to enable objects of a structure 
  464. type or class type to be printed easily. In C, structures 
  465. may not be compared in their entirety; they must be 
  466. compared member by member. Chapter 8 also 
  467. illustrates how to overload equality operators and 
  468. relational operators to compare objects of (C++) 
  469. structure and class types. <br>
  470. <spacer width=16 height=1>The following section reimplements our <b>Time</b> structure 
  471. as a C++ class and demonstrates some of the advantages <br>
  472.  
  473. </page>
  474. <page>
  475. to creating so-called <i>abstract data types</i> as classes. We 
  476. will see that classes and structures can be used almost 
  477. identically in C++. The difference between the two is in 
  478. the default accessibility associated with the members of 
  479. each. This will be explained shortly.<br>
  480.  
  481. </page>
  482. <page>
  483. <b>Select the true statement(s). </b><br>
  484. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Each member must be printed individually.">
  485. A structure can be printed as a unit.   <br>
  486. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  487. A structure's data is directly accessible to a program. <br>
  488. <component type=button name=b label="Check Your Answer" width=125 height=24>
  489.  
  490. </page>
  491. </section>
  492. <section type=Body name=Default title="6.5 Implementing a Time Abstract Data Type with a Class">
  493. <page>
  494. <font size=18 bold>6.5 Implementing a Time Abstract Data 
  495. Type with a Class</font><hr>
  496. Classes enable the programmer to model objects that 
  497. have <i>attributes</i> (represented as <i>data members</i>) and 
  498. <i>behaviors</i> or <i>operations</i> (represented as <i>member 
  499. functions</i>). Types containing data members and member 
  500. functions are defined in C++ using the keyword <b>class</b>.<br>
  501. <spacer width=16 height=1>Member functions are sometimes called <i>methods</i> in 
  502. other object-oriented programming languages, and are 
  503. invoked in response to <i>messages</i> sent to an object. A 
  504. message corresponds to a member-function call sent <br>
  505.  
  506. </page>
  507. <page>
  508. from one object to another or sent from a function to an 
  509. object.<br>
  510. <spacer width=16 height=1>Once a class has been defined, the class name can be 
  511. used to declare objects of that class.  <a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 6.2</a> 
  512. contains a simple definition for class <b>Time</b>.<br>
  513. <spacer width=16 height=1>Our <b>Time</b> class definition begins with the keyword 
  514. <b>class</b>. The <i>body</i> of the class definition is delineated with 
  515. left and right braces (<b>{</b> and <b>}</b>). <a href="^Errors::c:s0p2"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>The class definition 
  516. terminates with a semicolon. Our <b>Time</b> class definition 
  517. and our <b>Time</b> structure definition each contain the three 
  518. integer members <b>hour</b>, <b>minute</b>, and <b>second</b>. <br>
  519. <spacer width=16 height=1>The remaining parts of the class definition are new. The 
  520. <b>public:</b> and <b>private:</b> labels are called <i>member access</i> <br>
  521.  
  522. </page>
  523. <page>
  524. <i>specifiers</i>. Any data member or member function 
  525. declared after member access specifier <b>public</b> (and 
  526. before the next member access specifier) is accessible 
  527. wherever the program has access to an object of class 
  528. <b>Time</b>. Any data member or member function declared 
  529. after member access specifier <b>private</b> (and up to the 
  530. next member access specifier) is accessible only to 
  531. member functions of the class. <a href="^Practice::c:s0p0"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>Member access 
  532. specifiers are always followed by a colon (<b>:</b>) and can 
  533. appear multiple times and in any order in a class 
  534. definition. For the remainder of the text, we will refer to 
  535. the member access specifiers as <b>public</b> and <b>private</b> 
  536. (without the colon). In Chapter 9 we introduce a third <br>
  537.  
  538. </page>
  539. <page>
  540. member access specifier, <b>protected</b>, as we study 
  541. inheritance and the part it plays in object-oriented 
  542. programming.<br>
  543. <spacer width=16 height=1>The class definition contains prototypes for the 
  544. following four member functions after the <b>public</b> 
  545. member access specifier--<b>Time</b>, <b>setTime</b>, 
  546. <b>printMilitary</b>, and <b>printStandard</b>. These are the 
  547. <b><i>public</i></b> <i>member functions</i>, or <b><i>public</i></b> <i>services</i>, or <b><i>public</i></b> 
  548. <i>behaviors</i> or <i>interface</i> of the class. These functions will 
  549. be used by <i>clients</i> (i.e., portions of a program that are 
  550. users) of the class to manipulate the data of the class. <br>
  551. <spacer width=16 height=1>Notice the member function with the same name as the 
  552. class; it is called a <i>constructor</i> function of that class. A <br>
  553.  
  554. </page>
  555. <page>
  556. constructor is a special member function that initializes 
  557. the data members of a class object. A class's 
  558. constructor function is called automatically when an 
  559. object of that class is created. We will see that it is 
  560. common to have several constructors for a class; this is 
  561. accomplished through function overloading.  <a href="^Errors::c:s0p3"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Note that 
  562. no return type is specified for the constructor.<br>
  563. <spacer width=16 height=1>The three integer members appear after the <b>private</b> 
  564. member access specifier. This indicates that these data 
  565. members of the class are only accessible to member 
  566. functions--and, as we will see in the next chapter, 
  567. "friends"--of the class. Thus, the data members can 
  568. only be accessed by the four functions whose <br>
  569.  
  570. </page>
  571. <page>
  572. prototypes appear in the class definition (or by friends 
  573. of the class). Data members are normally listed in the 
  574. <b>private</b> portion of a class and member functions are 
  575. normally listed in the <b>public</b> portion. It is possible to 
  576. have <b>private</b> member functions and <b>public</b> data as we 
  577. will see later; the latter is uncommon and is considered 
  578. a poor programming practice.<br>
  579. <spacer width=16 height=1>Once the class has been defined, it can be used as a type 
  580. in declarations as follows:<br>
  581. <font size=2><br></font><font size=11><pre>
  582. Time sunset,<p>
  583.          // object of type Time<p>
  584.      arrayOfTimes[ 5 ], <p>
  585.          // array of Time objects<p>
  586.      *pointerToTime,<p><p>
  587. </pre></font>
  588.  
  589. </page>
  590. <page>
  591. <font size=2><br></font><font size=11><pre>
  592.           // pointer to a Time object<p>
  593.      &dinnerTime = sunset;<p>
  594.           // reference to a Time object<p>
  595. </pre></font>
  596. The class name becomes a new type specifier. There 
  597. may be many objects of a class, just as there may be 
  598. many variables of a type such as int. The programmer 
  599. can create new class types as needed. This is one reason 
  600. why C++ is said to be an <i>extensible language</i>.<br>
  601. <spacer width=16 height=1><a href="^Code::c:s0p2"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 6.3</a> uses the <b>Time</b> class. The program 
  602. instantiates a single object of class <b>Time</b> called <b>t</b>. When 
  603. the object is instantiated, the <b>Time</b> constructor is called 
  604. automatically and explicitly initializes each <b>private</b> 
  605. data member to <b>0</b>. The time is then printed in military <br>
  606.  
  607. </page>
  608. <page>
  609. and standard formats to confirm that the members have 
  610. been initialized properly. The time is then set using the 
  611. <b>setTime</b> member function and is printed again in both 
  612. formats. Then <b>setTime</b> attempts to set the data 
  613. members to invalid values, and the time is again printed 
  614. in both formats.<br>
  615.  Again, note that the data members <b>hour</b>, <b>minute</b>, and 
  616. <b>second</b> are preceded by the <b>private</b> member access 
  617. specifier. A class's <b>private</b> data members are normally 
  618. not accessible outside the class. (Again, we will see in 
  619. Chapter 7 that friends of a class may access the class's 
  620. <b>private</b> members.) The philosophy here is that the actual 
  621. data representation used within the class is of no <br>
  622.  
  623. </page>
  624. <page>
  625. concern to the class's clients. For example, it would be 
  626. perfectly reasonable for the class to represent the time 
  627. internally as the number of seconds since midnight. 
  628. Clients could use the same <b>public</b> member functions 
  629. and get the same results without being aware of this. In 
  630. this sense, the implementation of a class is said to be 
  631. <i>hidden</i> from its clients.  <a href="^Engineer::c:s0p3"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Such <i>information hiding</i> 
  632. promotes program modifiability and simplifies the 
  633. client's perception of a class. <br>
  634. <spacer width=16 height=1>In this program, the <b>Time</b> constructor simply initializes 
  635. the data members to 0 (i.e., the military time equivalent 
  636. of 12 AM). This ensures that the object is in a 
  637. consistent state when it is created. <a href="^Engineer::c:s0p5"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Invalid values cannot <br>
  638.  
  639. </page>
  640. <page>
  641. be stored in the data members of a <b>Time</b> object because 
  642. the constructor is automatically called when the <b>Time</b> 
  643. object is created and all subsequent attempts by a client 
  644. to modify the data members are scrutinized by function 
  645. <b>setTime</b>.<br>
  646. <spacer width=16 height=1> <a href="^Errors::c:s0p4"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Note that the data members of a class cannot be 
  647. initialized where they are declared in the class body. 
  648. These data members should be initialized by the class's 
  649. constructor, or they can be assigned values by "set" 
  650. functions.<br>
  651. <spacer width=16 height=1>A function with the same name as the class but 
  652. preceded with a <i>tilde character (~)</i> is called the 
  653. <i>destructor</i> of that class (this example does not explicitly <br>
  654.  
  655. </page>
  656. <page>
  657. include a destructor so the system "plugs one in" for 
  658. you). The destructor does "termination housekeeping" 
  659. on each class object before the memory for the object is 
  660. reclaimed by the system. Destructors cannot take 
  661. arguments and hence cannot be overloaded. We will 
  662. discuss constructors and destructors in more detail later 
  663. in this chapter and in Chapter 7.<br>
  664. <spacer width=16 height=1>Note that the functions the class provides to the outside 
  665. world are preceded by the <b>public</b> label. The <b>public</b> 
  666. functions implement the behaviors or services the class 
  667. provides to its clients--commonly referred to as the 
  668. <a href="^Engineer::c:s0p8"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>class's <i>interface</i> or <b><i>public</i></b> <i>interface</i>. <br>
  669.  
  670. </page>
  671. <page>
  672. The class definition contains declarations of the class's 
  673. data members and the class's member functions. The 
  674. member function declarations are the function 
  675. prototypes we discussed in earlier chapters. Member 
  676. functions can be defined inside a class,  <a href="^Engineer::c:s0p9"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>but it is a good 
  677. programming practice to define the functions outside 
  678. the class definition.<br>
  679. <spacer width=16 height=1>Note the use of the <i>binary scope resolution operator(<tt><b>::</b>)</tt></i> 
  680. in each member function definition following the class 
  681. definition in  <a href="^Code::c:s0p2"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.3</a>. Once a class is defined and its 
  682. member functions are declared, the member functions 
  683. must be defined. Each member function of the class can 
  684. be defined directly in the class body (rather than <br>
  685.  
  686. </page>
  687. <page>
  688. including the function prototype of the class), or the 
  689. member function can be defined after the class body.  
  690. <a href="^Errors::c:s0p5"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>When a member function is defined after its 
  691. corresponding class definition, the function name is 
  692. preceded by the class name and the binary scope 
  693. resolution operator (<b>::</b>). Because different classes can 
  694. have the same member names, the scope resolution 
  695. operator "ties" the member name to the class name to 
  696. uniquely identify the member functions of a particular 
  697. class. <br>
  698. <spacer width=16 height=1>Even though a member function declared in a class 
  699. definition may be defined outside that class definition, 
  700. that member function is still within that <i>class's scope</i>, <br>
  701.  
  702. </page>
  703. <page>
  704. i.e., its name is known only to other members of the 
  705. class unless referred to via an object of the class, a 
  706. reference to an object of the class, or a pointer to an 
  707. object of the class. We will say more about class scope 
  708. shortly.<br>
  709. <spacer width=16 height=1> <a href="^Perform::c:s0p1"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>If a member function is defined in a class definition, 
  710. the member function is automatically inlined.  <a href="^Engineer::c:s0p11"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Member 
  711. functions defined outside a class definition may be 
  712. made inline by explicitly using the keyword <b>inline</b>. 
  713. Remember that the compiler reserves the right not to 
  714. inline any function.<br>
  715. <spacer width=16 height=1>It is interesting that the <b>printMilitary</b> and 
  716. <b>printStandard</b> member functions take no arguments. <br>
  717.  
  718. </page>
  719. <page>
  720. This is because member functions implicitly know that 
  721. they are to print the data members of the particular 
  722. <b>Time</b> object for which they are invoked. <a href="^Engineer::c:s0p12"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>This makes 
  723. member function calls more concise than conventional 
  724. function calls in  <a href="^Debug::c:s0p0"><img src="bckgrnds/icons/dbt_ico.gif" align=sidebar></a>procedural programming. <br>
  725. <spacer width=16 height=1>Classes simplify programming because the client (or 
  726. user of the class object) need only be concerned with 
  727. the operations encapsulated or embedded in the object. 
  728. Such operations are usually designed to be client-
  729. oriented rather than implementation-oriented. Clients 
  730. need not be concerned with a class's implementation 
  731. (although the client, of course, wants a correct and 
  732. efficient implementation).  <a href="^Engineer::c:s0p14"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Interfaces do change, but <br>
  733.  
  734. </page>
  735. <page>
  736. less frequently than implementations. When an 
  737. implementation changes, implementation-dependent 
  738. code must change accordingly. By hiding the 
  739. implementation we eliminate the possibility of other 
  740. program parts becoming dependent on the details of the 
  741. class implementation.<br>
  742. <spacer width=16 height=1>Often, classes do not have to be created "from scratch." 
  743. Rather, they may be <i>derived</i> from other classes that 
  744. provide attributes and behaviors the new classes can 
  745. use. Or classes can include objects of other classes as 
  746. members. Such <i>software reuse</i> can greatly enhance 
  747. programmer productivity. Deriving new classes from 
  748. existing classes is called <i>inheritance</i> and is discussed in <br>
  749.  
  750. </page>
  751. <page>
  752. detail in Chapter 9. Including class objects as members 
  753. of other classes is called <i>composition</i> and is discussed 
  754. in Chapter 7.<br>
  755. <spacer width=16 height=1>People new to object-oriented programming often 
  756. express concern at the fact that objects must be quite 
  757. large because they contain data and functions. 
  758. Logically, this is true--the programmer may think of 
  759. objects as containing data and functions. <a href="^Perform::c:s0p3"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>Physically, 
  760. however, this is not true.<br>
  761.  
  762. </page>
  763. <page>
  764. <b>Select the true statement(s). </b><br>
  765. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. All member functions have class scope.">
  766. A member function defined outside of the class where it is declared does not have class scope.   <br>
  767. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  768. Composition is when a class definition contains an object of another class.  <br>
  769. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. The body can be defined in the class.">
  770. A member function's body cannot be defined in the class in which it is declared.   <br>
  771. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. It is called the destructor function.">
  772. A member function with the same name as the class and preceded with a ~ is called the constructor function. <br>
  773. <component type=button name=b label="Check Your Answer" width=125 height=24>
  774.  
  775. </page>
  776. </section>
  777. <section type=Body name=Default title="6.6 Class Scope and Accessing Class Members ">
  778. <page>
  779. <font size=18 bold>6.6 Class Scope and Accessing Class Members </font><hr>
  780. A class's data members (variables declared in the class 
  781. definition) and member functions (functions declared in 
  782. the class definition) belong to that <i>class's scope</i>. 
  783. Nonmember functions are defined at <i>file scope</i>.<br>
  784. <spacer width=16 height=1>Within a class's scope, class members are immediately 
  785. accessible by all of that class's member functions and 
  786. can be referenced by name. Outside a class's scope, 
  787. class members are referenced through one of the 
  788. handles on an object--an object name, a reference to an 
  789. object, or a pointer to an object. [We will see in Chapter <br>
  790.  
  791. </page>
  792. <page>
  793. 7 that an implicit handle is inserted by the compiler on 
  794. every reference to a data member or member function 
  795. in an object.]<br>
  796. <spacer width=16 height=1>Member functions of a class can be overloaded, but 
  797. only by other member functions of the class. To 
  798. overload a member function, simply provide in the 
  799. class definition a prototype for each version of the 
  800. overloaded function, and provide a separate function 
  801. definition for each version of the function.<br>
  802. <spacer width=16 height=1>Member functions have <i>function scope</i> in a class--
  803. variables defined in a member function are known only 
  804. to that function. If a member function defines a variable 
  805. with the same name as a variable with class scope, the <br>
  806.  
  807. </page>
  808. <page>
  809. class-scope variable is hidden by the function-scope 
  810. variable in the function scope. Such a hidden variable 
  811. can be accessed by preceding the operator with the class 
  812. name followed by the scope resolution operator (<b>::</b>). 
  813. Hidden global variables can be accessed with the unary 
  814. scope resolution operator (see Chapter 3).<br>
  815. <spacer width=16 height=1>The operators used to access class members are 
  816. identical to the operators used to access structure 
  817. members. The <i>dot member selection operator</i> (<b>.</b>) is 
  818. combined with an object's name or with a reference to 
  819. an object to access the object's members. The<i> arrow 
  820. member selection operator</i> (<b>-></b>) is combined with a 
  821. pointer to an object to access that object's members. <br>
  822.  
  823. </page>
  824. <page>
  825. The program of  <a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.4</a> uses a simple class called 
  826. <b>Count</b> with <b>public</b> data member <b>x</b> of type <b>int</b>, and 
  827. <b>public</b> member function <b>print</b> to illustrate accessing 
  828. the members of a class with the member selection 
  829. operators. The program instantiates three variables 
  830. related to type <b>Count</b>--<b>counter</b>, <b>counterRef</b> (a 
  831. reference to a <b>Count</b> object), and <b>counterPtr</b> (a pointer 
  832. to a <b>Count</b> object). Variable <b>counterRef</b> is defined to 
  833. reference <b>counter</b>, and variable <b>counterPtr</b> is defined 
  834. to point to <b>counter</b>. <i>It is important to note that data 
  835. member <b>x</b> has been made <b>public</b> here simply to 
  836. demonstrate how <b>public</b> members are accessed off 
  837. handles (i.e., a name, a reference or a pointer). As we</i> <br>
  838.  
  839. </page>
  840. <page>
  841. <i>have stated, data is typically made <b>private</b> as we will do 
  842. in most subsequent examples.</i> In Chapter 9, 
  843. "Inheritance," we will sometimes make data <b>protected</b>.<br>
  844.  
  845. </page>
  846. <page>
  847. <b>Select the true statement(s). </b><br>
  848. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  849. Nonmember functions are defined at file scope.  <br>
  850. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Member functions can be overloaded.">
  851. Member functions cannot be overloaded.   <br>
  852. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  853. Operators . and -> are used to access class members.  <br>
  854. <component type=button name=b label="Check Your Answer" width=125 height=24>
  855.  
  856. </page>
  857. </section>
  858. <section type=Body name=Default title="6.7 Separating Interface from Implementation">
  859. <page>
  860. <font size=18 bold>6.7 Separating Interface from Implementation</font><hr>
  861. One of the fundamental principles of good software 
  862. engineering is to separate interface from 
  863. implementation.  <a href="^Engineer::c:s0p19"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>This makes it easier to modify 
  864. programs. As far as clients of a class are concerned, 
  865. changes in the <a href="^Engineer::c:s0p16"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>class's implementation do not affect the 
  866. client as long as the class's interface originally provided 
  867. to the client is unchanged (the class's functionality 
  868. could be expanded beyond the original interface). <br>
  869. <spacer width=16 height=1>Actually, things are not quite this rosy. Header files do 
  870. contain some portion of the implementation and hints <br>
  871.  
  872. </page>
  873. <page>
  874. about other portions of the implementation. Inline 
  875. member functions, for example, need to be in a header 
  876. file, so that when the compiler compiles a client, the 
  877. client can include the <b>inline</b> function definition in 
  878. place.  <a href="^Engineer::c:s0p21"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Private members are listed in the class definition 
  879. in the header file, so these members are visible to 
  880. clients even though the clients may not access the 
  881. <b>private</b> members. In Chapter 7, we show how to use a 
  882. so-called <i>proxy class</i> to hide even the <b>private</b> data of a 
  883. class from clients of the class.<br>
  884. <spacer width=16 height=1> <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 6.5</a> splits the program of <a href="^Code::c:s0p2"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.3</a> into multiple 
  885. files. When building a C++ program, each class 
  886. definition is normally placed in a <i>header file</i>, and that <br>
  887.  
  888. </page>
  889. <page>
  890. class's member function definitions are placed in 
  891. <i>source-code files</i> of the same base name. The header 
  892. files are included (via <b>#include</b>) in each file in which 
  893. the class is used, and the source-code file is compiled 
  894. and linked with the file containing the main program. 
  895. See your compiler's documentation to determine how 
  896. to compile and link programs consisting of multiple 
  897. source files.<br>
  898. <spacer width=16 height=1><a href="^Code::c:s0p4"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 6.5</a> consists of the header file <b>time1.h</b> in which 
  899. class <b>Time</b> is declared, the file <b>time1.cpp</b> in which the 
  900. member functions of class <b>Time</b> are defined, and the 
  901. file f<b>ig06_05.cpp</b> in which function <b>main</b> is defined. <br>
  902.  
  903. </page>
  904. <page>
  905. The output for this program is identical to the output of  
  906. <a href="^Code::c:s0p2"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.3</a>.<br>
  907. <spacer width=16 height=1>Note that the class declaration is enclosed in the 
  908. following preprocessor code:<br>
  909. <font size=2><br></font><font size=11><pre>
  910. // prevent multiple inclusions of header file<p>
  911. #ifndef TIME1_H<p>
  912. #define TIME1_H<p>
  913.    ...<p>
  914. #endif<p>
  915. </pre></font>
  916. When we build larger programs, other definitions and 
  917. declarations will also be placed in header files. The 
  918. preceding preprocessor directives prevent the code 
  919. between <b>#ifndef</b> and <b>#endif</b> from being included if the 
  920. name <b>TIME1_H</b> has been defined. If the header has not <br>
  921.  
  922. </page>
  923. <page>
  924. been included previously in a file, the name <b>TIME1_H</b> 
  925. is defined by the <b>#define</b> directive and the header file 
  926. statements are included. If the header has been included 
  927. previously, <b>TIME1_H</b> is defined already and the 
  928. header file is not included again.  <a href="^Debug::c:s0p2"><img src="bckgrnds/icons/dbt_ico.gif" align=sidebar></a>Attempts to include a 
  929. header file multiple times (inadvertently) typically 
  930. occur in large programs with many header files that 
  931. may themselves include other header files.  <a href="^Practice::c:s0p1"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>Note: The 
  932. convention we use for the symbolic constant name in 
  933. the preprocessor directives is simply the header file 
  934. name with the underscore character replacing the 
  935. period.<br>
  936.  
  937. </page>
  938. <page>
  939. <b>Select the true statement(s). </b><br>
  940. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Most of the implementation is provided in separate source files.">
  941. Header files provide the complete definition of a class's implementation details.   <br>
  942. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  943. Each class definition is normally placed in a header file.  <br>
  944. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Header files need to be included.">
  945. Header files written by a programmer do not require the use of the preprocessor directive #include. <br>
  946. <component type=button name=b label="Check Your Answer" width=125 height=24>
  947.  
  948. </page>
  949. </section>
  950. <section type=Body name=Default title="6.8 Controlling Access to Members ">
  951. <page>
  952. <font size=18 bold>6.8 Controlling Access to Members </font><hr>
  953. The member access specifiers <b>public</b> and <b>private</b> (and 
  954. <b>protected</b> as we will see in Chapter 9, "Inheritance") 
  955. are used to control access to a class's data members and 
  956. member functions. <a href="^Practice::c:s0p4"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>The default access mode for classes 
  957. is <b>private</b> so all members after the class header and 
  958. before the first label are <b>private</b>. After each label, the 
  959. mode that was invoked by that label applies until the 
  960. next label or until the terminating right brace (<b>}</b>) of the 
  961. class definition.  <a href="^Practice::c:s0p2"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>The labels <b>public</b>, <b>private</b>, and 
  962. <b>protected</b> may be repeated, but such usage is rare and 
  963. can be confusing.<br>
  964.  
  965. </page>
  966. <page>
  967. A class's <b>private</b> members can be accessed only by 
  968. member functions (and <b>friends</b>, as we will see in 
  969. Chapter 7) of that class. The <b>public</b> members of a class 
  970. may be accessed by any function in the program.<br>
  971. <spacer width=16 height=1>The primary purpose of <b>public</b> members is to present to 
  972. the class's clients a view of the <i>services</i> (behaviors) the 
  973. class provides. This set of services forms the <b><i>public</i></b> 
  974. <i>interface</i> of the class. Clients of the class need not be 
  975. concerned with how the class accomplishes its tasks. 
  976. <a href="^Errors::c:s0p6"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>The <b>private</b> members of a class as well as the 
  977. definitions of its <b>public</b> member functions are not 
  978. accessible to the clients of a class. These components 
  979. <a href="^Engineer::c:s0p23"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>form the <i>implementation</i> of the class. <br>
  980.  
  981. </page>
  982. <page>
  983. <a href="^Code::c:s0p5">F <img src="bckgrnds/icons/code_ico.gif" align=sidebar>igure 6.6</a> demonstrates that <b>private</b> class members 
  984. are only accessible through the <b>public</b> class interface 
  985. using <b>public</b> <a href="^Engineer::c:s0p25"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>member functions. When this program is 
  986. compiled, the compiler generates two errors stating that 
  987. the <b>private</b> member specified in each statement is not 
  988. accessible. <a href="^Code::c:s0p5"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 6.6</a> includes <b>time1.h</b> and is compiled 
  989. with <b>time1.cpp</b> from  <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.5</a>.<br>
  990. <spacer width=16 height=1>A client of a class may be a member function of another 
  991. class or it may be a global function (i.e., a C-like 
  992. "loose" function in the file that is not a member 
  993. function of any class).<br>
  994. <spacer width=16 height=1>The default access for members of a class is <b>private</b>. 
  995. Access to members of a class may be explicitly set to <br>
  996.  
  997. </page>
  998. <page>
  999. <b>public</b>, <b>protected</b> (as we will see in Chapter 9), or 
  1000. <b>private</b>. The default access for struct members is 
  1001. <b>public</b>. Access to members of a <b>struct</b> also may be 
  1002. explicitly set to <b><img src="bckgrnds/icons/seo_ico.gif" align=sidebar>public</b>, <b>protected</b>, or <b>private</b>. <br>
  1003. <spacer width=16 height=1>Just because class data is <b>private</b> does not necessarily 
  1004. mean that clients cannot effect changes to that data. The 
  1005. data can be changed by member functions or <tt><b>friend</b></tt>s of 
  1006. that class. As we will see, these functions should be 
  1007. designed to ensure the integrity of the data. <br>
  1008. <spacer width=16 height=1><a href="^Debug::c:s0p3"><img src="bckgrnds/icons/dbt_ico.gif" align=sidebar></a>Access to a class's <b>private</b> data should be carefully 
  1009. controlled by the use of member functions, called 
  1010. <i>access functions</i> (also called <i>accessor methods</i>). For 
  1011. example, to allow clients to read the value of <b>private</b> <br>
  1012.  
  1013. </page>
  1014. <page>
  1015. data, the class can provide a <i>get</i> function. To enable 
  1016. clients to modify <tt><b>private</b></tt> data, the class can provide 
  1017. a <i>set</i> function. Such modification would seem to violate 
  1018. the notion of <b>private</b> data.  <a href="^Engineer::c:s0p28"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>But a <i>set</i> member function 
  1019. can provide data validation capabilities (such as range 
  1020. checking) to ensure that the value is set properly. A <i>set</i> 
  1021. function can also translate between the form of the data 
  1022. used in the interface and the form used in the 
  1023. implementation. A <i>get</i> function need not expose the 
  1024. data in "raw" format; rather the <i>get</i> function can edit the 
  1025. data and limit the view of the data the client will see.<br>
  1026.  
  1027. </page>
  1028. <page>
  1029. <b>Select the true statement(s). </b><br>
  1030. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1031. The default access mode for members of a class is private.  <br>
  1032. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1033. The labels public, private, and protected can be repeated in a class definition. <br>
  1034. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. public members are accessible both inside and outside the class.">
  1035. Member functions designated public can only be accessed from outside the class.   <br>
  1036. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1037. Access to members of a struct can be designated public, protected, or private.  <br>
  1038. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1039.  
  1040. </page>
  1041. </section>
  1042. <section type=Body name=Default title="6.9 Access Functions and Utility Functions">
  1043. <page>
  1044. <font size=18 bold>6.9 Access Functions and Utility Functions</font><hr>
  1045. Not all member functions need be made <b>public</b> to serve 
  1046. as part of the interface of a class.  <a href="^Engineer::c:s0p29"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Some member 
  1047. functions remain <b>private</b> and serve as <i>utility functions</i> 
  1048. to the other functions of the class.<br>
  1049. <spacer width=16 height=1>Access functions can read or display data. Another 
  1050. common use for access functions is to test the truth or 
  1051. falsity of conditions--such functions are often called 
  1052. <i>predicate functions</i>. An example of a predicate function 
  1053. would be an <b>isEmpty</b> function for any container 
  1054. class--a class capable of holding many objects--such 
  1055. as a linked list, a stack or a queue. A program would <br>
  1056.  
  1057. </page>
  1058. <page>
  1059. test <b>isEmpty</b> before attempting to read another item 
  1060. from the container object. An <b>isFull</b> predicate function 
  1061. might test a container class object to determine if it has 
  1062. no additional room. A set of useful predicate functions 
  1063. for our <b>Time</b> class might be <b>isAM</b> and <b>isPM</b>.<br>
  1064. <spacer width=16 height=1><a href="^Code::c:s0p6"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 6.7</a> demonstrates the notion of a <i>utility function</i> 
  1065. (also called a <i>helper function</i>). A utility function is not 
  1066. part of a class's interface; rather, it is a <b>private</b> member 
  1067. function that supports the operation of the class's 
  1068. <b>public</b> member functions. Utility functions are not 
  1069. intended to be used by clients of a class. <br>
  1070. <spacer width=16 height=1>Class <b>SalesPerson</b> has an array of 12 monthly sales 
  1071. figures initialized by the constructor to zero and set to <br>
  1072.  
  1073. </page>
  1074. <page>
  1075. user-supplied values by function <b>setSales</b>. Public 
  1076. member function <b>printAnnualSales</b> prints the total 
  1077. sales for the last 12 months. Utility function 
  1078. <b>totalAnnualSales</b> totals the 12 monthly sales figures 
  1079. for the benefit of <b>printAnnualSales</b>. Member function 
  1080. <b>printAnnualSales</b> edits the sales figures into dollar 
  1081. amount format. <br>
  1082. <spacer width=16 height=1><a href="^Engineer::c:s0p32"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Note that <b>main</b> includes only a simple sequence of 
  1083. member function calls--there are no control structures.<br>
  1084.  
  1085. </page>
  1086. <page>
  1087. <b>Select the true statement(s). </b><br>
  1088. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1089. private member functions are often referred to as utility functions.  <br>
  1090. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1091. Functions that return boolean values are often referred to as predicate functions. <br>
  1092. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Utility functions are for use by other member functions of the class.">
  1093. Utility functions are designed to called by clients of the class.   <br>
  1094. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1095.  
  1096. </page>
  1097. </section>
  1098. <section type=Body name=Default title="6.10 Initializing Class Objects: Constructors">
  1099. <page>
  1100. <font size=18 bold>6.10 Initializing Class Objects: Constructors</font><hr>
  1101. When a class object is created, its members can be 
  1102. initialized by that class's <i>constructor</i> function. A 
  1103. constructor is a class member function with the same 
  1104. name as the class.  <a href="^Practice::c:s0p6"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>The programmer provides the 
  1105. constructor which is then invoked automatically each 
  1106. <a href="^Debug::c:s0p5"><img src="bckgrnds/icons/dbt_ico.gif" align=sidebar></a>time an object of that class is created (instantiated). 
  1107. Constructors may be overloaded to provide a variety of 
  1108. means for initializing objects of a class. <a href="^Errors::c:s0p7"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Data members 
  1109. must either be initialized in a constructor of the class, or  
  1110. <a href="^Errors::c:s0p8"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>their values may be <i>set</i> later after the object is created. <br>
  1111.  
  1112. </page>
  1113. <page>
  1114. When an object of a class is declared, <i>initializers</i> can be 
  1115. provided in parentheses to the right of the object name 
  1116. and before the semicolon. These initializers are passed 
  1117. as arguments to the class's constructor. We will soon 
  1118. see several examples of these <i>constructor calls</i>. [Note: 
  1119. Although programmers do not explicitly call 
  1120. constructors, programmers can still provide data that 
  1121. gets passed to constructors as arguments.]<br>
  1122.  
  1123. </page>
  1124. <page>
  1125. <b>Select the true statement(s). </b><br>
  1126. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Constructors never return values.">
  1127. Like any other function, constructors can return a value.   <br>
  1128. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Constructors can be overloaded.">
  1129. Constructors cannot be overloaded.   <br>
  1130. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1131. A constructor is a member function with the same name as the class.  <br>
  1132. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1133.  
  1134. </page>
  1135. </section>
  1136. <section type=Body name=Default title="6.11 Using Default Arguments with Constructors">
  1137. <page>
  1138. <font size=18 bold>6.11 Using Default Arguments with Constructors</font><hr>
  1139. The constructor from <b>time1.cpp</b> ( <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.5</a>) initialized 
  1140. <b>hour</b>, <b>minute</b>, and <b>second</b> to <b>0</b> (i.e., 12 midnight in 
  1141. military time). Constructors can contain default 
  1142. arguments. <a href="^Code::c:s0p7"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 6.8</a> redefines the <b>Time</b> constructor 
  1143. function to include default arguments of zero for each 
  1144. variable. By providing default arguments to the 
  1145. constructor, even if no values are provided in a 
  1146. constructor call, the object is still guaranteed to be 
  1147. initialized to a consistent state due to the default 
  1148. arguments. A programmer-supplied constructor that <br>
  1149.  
  1150. </page>
  1151. <page>
  1152. defaults all its arguments (or explicitly requires no 
  1153. arguments) is also a <i>default constructor</i>, i.e., a 
  1154. constructor that can be invoked with no arguments. 
  1155. There can be only one default constructor per class. <br>
  1156. In this program, the constructor calls member function 
  1157. <b>setTime</b> with the values passed to the constructor (or 
  1158. the default values) to ensure that the value supplied for 
  1159. <b>hour</b> is in the range 0 to 23, and that the values for 
  1160. <b>minute</b> and <b>second</b> are each in the range 0 to 59. If a 
  1161. value is out of range, it is set to zero by <b>setTime</b> (this is 
  1162. an example of ensuring that a data member remains in a 
  1163. consistent state). <br>
  1164.  
  1165. </page>
  1166. <page>
  1167. Note that the <b>Time</b> constructor could be written to 
  1168. include the same statements as member function 
  1169. <b>setTime</b>. This may be slightly more efficient because 
  1170. the extra call to <b>setTime</b> is eliminated. However, 
  1171. coding the <b>Time</b> constructor and member function 
  1172. <b>setTime</b> identically makes maintenance of this program 
  1173. more difficult. If the implementation of member 
  1174. function <b>setTime</b> changes, the implementation of the 
  1175. <b>Time</b> constructor should change accordingly. Having 
  1176. the <b>Time</b> constructor call <b>setTime</b> directly, requires any 
  1177. changes to the implementation of <b>setTime</b> to be made 
  1178. only once. <a href="^Engineer::c:s0p34"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>This reduces the likelihood of a 
  1179. programming error when altering the implementation. <br>
  1180.  
  1181. </page>
  1182. <page>
  1183. Also, the performance of the <b>Time</b> constructor can be 
  1184. enhanced by explicitly declaring the constructor <b>inline</b> 
  1185. or by defining  <a href="^Errors::c:s0p9"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>the constructor in the class definition 
  1186. (which implicitly  <b>inlines</b> the <b><a href="^Practice::c:s0p8"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a></b>function definition).<br>
  1187. <spacer width=16 height=1>The program of <a href="^Code::c:s0p7"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.8</a> initializes five <b>Time</b> objects--
  1188. one with all three arguments defaulted in the 
  1189. constructor call, one with one argument specified, one 
  1190. with two arguments specified, one with three arguments 
  1191. specified, and one with three invalid arguments 
  1192. specified. The contents of each object's data members 
  1193. after instantiation and initialization are displayed. <br>
  1194. <spacer width=16 height=1> <a href="^Engineer::c:s0p36"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>If no constructor is defined for a class, the compiler 
  1195. creates a default constructor. Such a constructor does <br>
  1196.  
  1197. </page>
  1198. <page>
  1199. not perform any initialization, so when the object is 
  1200. created, it is not guaranteed to be in a consistent state. <br>
  1201.  
  1202. </page>
  1203. <page>
  1204. <b>Select the true statement(s). </b><br>
  1205. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1206. A constructor can be inlined.   <br>
  1207. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1208. There can only be one default constructor per class.   <br>
  1209. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1210. Constructors can contain provide default arguments in the parameter list.   <br>
  1211. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1212.  
  1213. </page>
  1214. </section>
  1215. <section type=Body name=Default title="6.12  Using Destructors">
  1216. <page>
  1217. <font size=18 bold>6.12  Using Destructors</font><hr>
  1218. A <b>destructor</b> is a special member function of a class. 
  1219. The name of the destructor for a class is the <i>tilde</i> (<b>~</b>) 
  1220. character followed by the class name. This naming 
  1221. convention has intuitive appeal, because as we will see 
  1222. in a later chapter the tilde operator is the bitwise 
  1223. complement operator, and, in a sense, the destructor is 
  1224. the complement of the constructor. <br>
  1225. <spacer width=16 height=1>A class's destructor is called when an object is 
  1226. destroyed--e.g., when program execution leaves the 
  1227. scope in which an object of that class was instantiated. 
  1228. The destructor itself does not actually destroy the <br>
  1229.  
  1230. </page>
  1231. <page>
  1232. object--it performs <i>termination housekeeping</i> before 
  1233. the system reclaims the object's memory so that 
  1234. memory may be reused to hold new objects.<br>
  1235. <spacer width=16 height=1><a href="^Errors::c:s0p10"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>A destructor receives no parameters and returns no 
  1236. value. A class may have only one destructor--
  1237. destructor overloading is not allowed.<br>
  1238. <spacer width=16 height=1><a href="^Engineer::c:s0p37"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Notice that destructors have not been provided for the 
  1239. classes presented so far. We will soon see several 
  1240. examples of classes with useful destructors. In Chapter 
  1241. 8, we will see that destructors are appropriate for 
  1242. classes whose objects contain dynamically allocated 
  1243. memory (for arrays and strings, for example). In <br>
  1244.  
  1245. </page>
  1246. <page>
  1247. Chapter 7, we discuss how to dynamically allocate and 
  1248. deallocate storage.<br>
  1249.  
  1250. </page>
  1251. <page>
  1252. <b>Select the true statement(s). </b><br>
  1253. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1254. A destructor is called when an object is removed from memory.   <br>
  1255. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A destructor cannot take arguments.">
  1256. Like a constructor, a destructor can take arguments.   <br>
  1257. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A destructor cannot return a value.">
  1258. A destructor may return a value.   <br>
  1259. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1260.  
  1261. </page>
  1262. </section>
  1263. <section type=Body name=Default title="6.13 When Constructors and Destructors are Called">
  1264. <page>
  1265. <font size=18 bold>6.13 When Constructors and Destructors are 
  1266. Called</font><hr>
  1267. Constructors and destructors are called automatically. 
  1268. The order in which these function calls are made 
  1269. depends on the order in which execution enters and 
  1270. leaves the scope in which objects are instantiated. 
  1271. Generally, destructor calls are made in the reverse order 
  1272. of the constructor calls. However, as we will see in <a href="^Code::c:s0p8"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 
  1273. 6.9</a>, the storage class of objects can alter the order in 
  1274. which the destructors are called.<br>
  1275. <spacer width=16 height=1>Constructors are called for objects defined in global 
  1276. scope before any other function (including <b>main</b>) in <br>
  1277.  
  1278. </page>
  1279. <page>
  1280. that file begins execution (although the order of 
  1281. execution of global object constructors between files is 
  1282. not guaranteed). The corresponding destructors are 
  1283. called when <b>main</b> terminates or the <b>exit</b> function (see 
  1284. Chapter 18, "Other Topics") is called.<br>
  1285. <spacer width=16 height=1>Constructors are called for automatic local objects 
  1286. when execution reaches the point where the objects are 
  1287. defined. The corresponding destructors are called when 
  1288. the objects leave scope (i.e., the block in which they are 
  1289. defined is exited). Constructors and destructors for 
  1290. automatic objects are called each time the objects enter 
  1291. and leave scope.<br>
  1292.  
  1293. </page>
  1294. <page>
  1295. Constructors are called for <b>static</b> local objects only 
  1296. once when execution first reaches the point where the 
  1297. objects are defined. Corresponding destructors are 
  1298. called when <b>main</b> terminates or the <b>exit</b> function is 
  1299. called.<br>
  1300. <spacer width=16 height=1>The program of <a href="^Code::c:s0p8"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.9</a> demonstrates the order in 
  1301. which constructors and destructors are called for 
  1302. objects of type <b>CreateAndDestroy</b> in several scopes. 
  1303. The program defines <b>first</b> in global scope. Its 
  1304. constructor is called as the program begins execution 
  1305. and its destructor is called at program termination after 
  1306. all other objects are destroyed. <br>
  1307.  
  1308. </page>
  1309. <page>
  1310. Function <b>main</b> declares three objects. Objects <b>second</b> 
  1311. and <b>fourth</b> are local automatic objects, and object <b>third</b> 
  1312. is a <b>static</b> local object. The constructors for each of 
  1313. these objects are called when execution reaches the 
  1314. point where each object is declared. The destructors for 
  1315. objects <b>fourth</b> and <b>second</b> are called in that order when 
  1316. the end of <b>main</b> is reached. Because object <b>third</b> is 
  1317. <b>static</b>, it exists until program termination. The 
  1318. destructor for object <b>third</b> is called before the 
  1319. destructor for <b>first</b>, but after all other objects are 
  1320. destroyed. <br>
  1321. <spacer width=16 height=1>Function <b>create</b> declares three objects--<b>fifth</b> and 
  1322. <b>seventh</b> are local automatic objects, and <b>sixth</b> is a <br>
  1323.  
  1324. </page>
  1325. <page>
  1326. <b>static</b> local object. The destructors for objects <b>seventh</b> 
  1327. and <b>fifth</b> are called in that order when the end of <b>create</b> 
  1328. is reached. Because <b>sixth</b> is <b>static</b>, it exists until 
  1329. program termination. The destructor for <b>sixth</b> is called 
  1330. before the destructors for <b>third</b> and <b>first</b>, but after all 
  1331. other objects are destroyed. <br>
  1332.  
  1333. </page>
  1334. <page>
  1335. <b>Select the true statement(s). </b><br>
  1336. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1337. Constructors and destructors are called automatically.   <br>
  1338. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1339. In general, destructor calls are made in the reverse order of constructor calls.   <br>
  1340. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A programmer can provide a constructor or destructor as required for a class. If not provided, the compiler provides default definitions of each.">
  1341. The programmer must always provide a constructor and destructor for a class.   <br>
  1342. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1343.  
  1344. </page>
  1345. </section>
  1346. <section type=Body name=Default title="6.14 Using Data Members and Member Functions">
  1347. <page>
  1348. <font size=18 bold>6.14 Using Data Members and Member 
  1349. Functions</font><hr>
  1350. A class's <b>private</b> data members can be manipulated 
  1351. only by member functions (and <b>friend</b>s) of the class. A 
  1352. typical manipulation might be the adjustment of a 
  1353. customer's bank balance (e.g., a <b>private</b> data member 
  1354. of a class <b>BankAccount</b>) by a member function 
  1355. <b>computeInterest</b>. <br>
  1356. <spacer width=16 height=1>Classes often provide <b>public</b> member functions to 
  1357. allow clients of the class to <i>set</i> (i.e., write) or <i>get</i> (i.e., 
  1358. read) the values of <b>private</b> data members. These 
  1359. functions need not be called <i>set</i> and <i>get</i> specifically, but <br>
  1360.  
  1361. </page>
  1362. <page>
  1363. they often are. More specifically, a member function 
  1364. that <i>sets</i> data member <b>interestRate</b> would typically be 
  1365. named <b>setInterestRate</b>, and a member function that 
  1366. <i>gets</i> the <b>interestRate</b> would typically be called 
  1367. <b>getInterestRate</b>. Get functions are also commonly 
  1368. called "query" functions.<br>
  1369. <spacer width=16 height=1>It may seem that providing both <i>set</i> and <i>get</i> capabilities 
  1370. is essentially the same as making the data members 
  1371. <b>public</b>. This is yet another subtlety of C++ that makes 
  1372. the language so desirable for software engineering. If a 
  1373. data member is <b>public</b>, then the data member may be 
  1374. read or written at will by any function in the program. If 
  1375. a data member is <b>private</b>, a <tt><b>public</b></tt> <i>get</i> function <br>
  1376.  
  1377. </page>
  1378. <page>
  1379. would certainly seem to allow other functions to read 
  1380. the data at will but the <i>get</i> function could control the 
  1381. formatting and display of the data. <a href="^Practice::c:s0p9"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>A <tt><b>public</b></tt> <i>set</i> 
  1382. function could--and most likely would--carefully 
  1383. scrutinize any attempt to modify the value of the data 
  1384. member.  <a href="^Engineer::c:s0p39"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>This would ensure that the new value is 
  1385. appropriate for that data item. <a href="^Debug::c:s0p6"><img src="bckgrnds/icons/dbt_ico.gif" align=sidebar></a>For example, an attempt 
  1386. to <i>set</i> the day of the month to 37 could be rejected, an 
  1387. attempt to <i>set</i> a person's weight to a negative value 
  1388. could be rejected, an attempt to <i>set</i> a numeric quantity 
  1389. to an alphabetic value could be rejected, an attempt to 
  1390. <i>set</i> a grade on an exam to 185 (when the proper range is 
  1391. zero to 100) could be rejected, etc. <br>
  1392.  
  1393. </page>
  1394. <page>
  1395. The client of a class should be notified when an attempt 
  1396. is made to assign an invalid value to a data member. A 
  1397. class's <i>set</i> functions are often written to return values 
  1398. indicating that an attempt was made to assign invalid 
  1399. data to an object of the class. This enables clients of the 
  1400. class to test the return values of <i>set</i> functions to 
  1401. determine if the object they are manipulating is a valid 
  1402. object and to take appropriate action if the object is not 
  1403. <a href="^Errors::c:s0p11"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>valid. <br>
  1404. <spacer width=16 height=1><a href="^Code::c:s0p9"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 6.10</a> extends our <b>Time</b> class to include <i>get</i> and 
  1405. <i>set</i> functions for the <b>hour</b>, <b>minute</b>, and <b>second</b> <b>private</b> 
  1406. data members. The <i>set</i> functions strictly control the 
  1407. setting of the data members. Attempts to <i>set</i> any data <br>
  1408.  
  1409. </page>
  1410. <page>
  1411. member to an incorrect value cause the data member to 
  1412. be set to zero (thus leaving the data member in a 
  1413. consistent state). Each <i>get</i> function simply returns the 
  1414. appropriate data member's value. The program first 
  1415. uses the <i>set</i> functions to <i>set</i> the <b>private</b> data members 
  1416. of <b>Time</b> object t to valid values, then uses the <i>get</i> 
  1417. functions to retrieve the values for output. Next the <i>set</i> 
  1418. functions attempt to <i>set</i> the <b>hour</b> and <b>second</b> members 
  1419. to invalid values and the <b>minute</b> member to a valid 
  1420. value, and then the <i>get</i> functions retrieve the values for 
  1421. output. The output confirms that invalid values cause 
  1422. the data members to be <i>set</i> to zero. Finally, the program 
  1423. <i>sets</i> the time to <b>11:58:00</b> and increments the minute <br>
  1424.  
  1425. </page>
  1426. <page>
  1427. value by 3 with a call to function <b>incrementMinutes</b>. 
  1428. Function <b>incrementMinutes</b> is a nonmember function 
  1429. that uses the <i>get</i> and <i>set</i> member functions to increment 
  1430. the <b>minute</b> member properly. Although this works, it 
  1431. incurs the performance burden of issuing multiple 
  1432. function calls. In the next chapter, we discuss the notion 
  1433. of <b>friend</b> functions as a means of eliminating this 
  1434. performance burden.<br>
  1435. Using <i>set</i> functions is certainly important from a 
  1436. software engineering standpoint because they can 
  1437. perform validity checking.  <a href="^Engineer::c:s0p40"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Both <i>set</i> and <i>get</i> functions 
  1438. have another important software engineering 
  1439. advantage.<br>
  1440.  
  1441. </page>
  1442. <page>
  1443. <b>Select the true statement(s). </b><br>
  1444. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1445. Get and set functions allow private members to be indirectly accessed.   <br>
  1446. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Get functions are sometimes called query functions.">
  1447. Set functions are sometimes called query functions.   <br>
  1448. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1449. A set function should perform validity checking.   <br>
  1450. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1451.  
  1452. </page>
  1453. </section>
  1454. <section type=Body name=Default title="6.15 A Subtle Trap: Returning a Reference to a Private Data Member">
  1455. <page>
  1456. <font size=18 bold>6.15 A Subtle Trap: Returning a Reference to 
  1457. a Private Data Member</font><hr>
  1458. A reference to an object is an alias for the <i>name</i> of the 
  1459. object and hence may be used on the left side of an 
  1460. assignment statement. In this context, the reference 
  1461. makes a perfectly acceptable <i>lvalue</i> that can receive a 
  1462. value. <a href="^Practice::c:s0p10"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>One way to use this capability (unfortunately!) is 
  1463. to have a <b>public</b> member function of a class return a 
  1464. <b>non-const</b> reference to a <b>private</b> data member of that 
  1465. class.<br>
  1466. <spacer width=16 height=1><a href="^Code::c:s0p10"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 6.11</a> uses a simplified <b>Time</b> class to demonstrate 
  1467. returning a reference to a <b>private</b> data member. Such a <br>
  1468.  
  1469. </page>
  1470. <page>
  1471. return actually makes a call to function <b>badSetHour</b> an 
  1472. alias for the <b>private</b> data member <b>hour</b>! The function 
  1473. call can be used in any way that the <b>private</b> data 
  1474. member can be used, including as an <i>lvalue</i> in an 
  1475. assignment statement! <br>
  1476. The program begins by declaring <b>Time</b> object <b>t</b> and 
  1477. reference <b>hourRef</b> that is assigned the reference 
  1478. returned by the call <b>t.badSetHour(20)</b>. The program 
  1479. displays the value of the alias <b>hourRef</b>. Next, the alias 
  1480. is used to set the value of <b>hour</b> to 30 (an invalid value) 
  1481. and the value is displayed again. Finally, the function 
  1482. call itself is used as an <i>lvalue</i> and assigned the value 74 
  1483. (another invalid value), and the value is displayed.<br>
  1484.  
  1485. </page>
  1486. </section>
  1487. <section type=Body name=Default title="6.16 Assignment by Default Memberwise Copy">
  1488. <page>
  1489. <font size=18 bold>6.16 Assignment by Default Memberwise 
  1490. Copy</font><hr>
  1491. The assignment operator (<b>=</b>) can be used to assign an 
  1492. object to another object of the same type. Such 
  1493. assignment is by default performed by <i>memberwise 
  1494. copy</i>--each member of one object is copied 
  1495. individually to the same member in another object (see 
  1496. <a href="^Code::c:s0p11"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.12</a>). (Note: Memberwise copy can cause serious 
  1497. problems when used with a class whose data members 
  1498. contain dynamically allocated storage; in Chapter 8, 
  1499. "Operator Overloading," we will discuss these 
  1500. problems and show how to deal with them.)<br>
  1501.  
  1502. </page>
  1503. <page>
  1504. Objects may be passed as function arguments and may 
  1505. be returned from functions.  <a href="^Perform::c:s0p6"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>Such passing and returning 
  1506. is performed call-by-value by default--a copy of the 
  1507. object is passed or returned (we present several 
  1508. examples in Chapter 8, "Operator Overloading").<br>
  1509.  
  1510. </page>
  1511. <page>
  1512. <b>Select the true statement(s). </b><br>
  1513. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1514. Objects are passed by value by default.  <br>
  1515. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1516. Assigning an object to an object of the same type results in default memberwise copy.  <br>
  1517. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1518.  
  1519. </page>
  1520. </section>
  1521. <section type=Body name=Default title="6.17 Software Reusability">
  1522. <page>
  1523. <font size=18 bold>6.17 Software Reusability</font><hr>
  1524. People who write object-oriented programs concentrate 
  1525. on implementing useful classes. There is a tremendous 
  1526. opportunity to capture and catalog classes so that they 
  1527. can be accessed by large segments of the programming 
  1528. community. Many <i>class libraries</i> exist and others are 
  1529. being developed worldwide. There are efforts to make 
  1530. these libraries broadly accessible. Software is 
  1531. increasingly being constructed from existing, well-
  1532. defined, carefully tested, well-documented, portable, 
  1533. widely available components. This kind of software 
  1534. reusability speeds the development of powerful, high-<br>
  1535.  
  1536. </page>
  1537. <page>
  1538. quality software. <i>Rapid applications development 
  1539. (RAD)</i> through the mechanisms of reusable 
  1540. componentry has become an important field. <br>
  1541. <spacer width=16 height=1>Significant problems must be solved, however, before 
  1542. the full potential of software reusability can be realized. 
  1543. We need cataloging schemes, licensing schemes, 
  1544. protection mechanisms to ensure that master copies of 
  1545. classes are not corrupted, description schemes so that 
  1546. designers of new systems can determine if existing 
  1547. objects meet their needs, browsing mechanisms to 
  1548. determine what classes are available and how closely 
  1549. they meet software developer requirements, and the 
  1550. like. Many interesting research and development <br>
  1551.  
  1552. </page>
  1553. <page>
  1554. problems need to be solved. There is great motivation 
  1555. to solve these problems because the potential value of 
  1556. their solutions is enormous.<br>
  1557.  
  1558. </page>
  1559. </section>
  1560. <section type=Body name=Default title="6.18 Thinking About Objects: Programming the Classes for the Elevator Simulator">
  1561. <page>
  1562. <font size=18 bold>6.18 Thinking About Objects: Programming 
  1563. the Classes for the Elevator Simulator</font><hr>
  1564. In the "Thinking About Objects" sections of Chapters 1 
  1565. through 5 we introduced the fundamentals of object 
  1566. orientation and walked you through an object-oriented 
  1567. design for an elevator simulator. In the body of Chapter 
  1568. 6, we introduced the details of programming and using 
  1569. C++ classes. At this point, you are ready (and probably 
  1570. eager!) to begin programming your elevator simulator. <br>
  1571.  
  1572. </page>
  1573. </section>
  1574. <section type=Body name=Default title="6.19 Elevator Laboratory Assignment 5 ">
  1575. <page>
  1576. <font size=18 bold>6.19 Elevator Laboratory Assignment 5 </font><hr>
  1577. 1. For each of the classes you identified in the 
  1578. "Thinking About Objects" sections of Chapters 2 
  1579. through 5, write an appropriate C++ class definition. 
  1580. For each class, include both a header file and a member 
  1581. function definition source file.<br>
  1582. <spacer width=16 height=1>2. Write a driver program that tests each of these 
  1583. classes, and that attempts to run the complete elevator 
  1584. simulation. CAUTION: You will probably need to wait 
  1585. until you have studied Chapter 7, "Classes: Part II," 
  1586. before you will be able to complete a reasonable 
  1587. working version of your simulator--so be patient and <br>
  1588.  
  1589. </page>
  1590. <page>
  1591. implement only those portions of the elevator simulator 
  1592. that you can with the knowledge you have gained in 
  1593. Chapter 6. In Chapter 7 you will learn about 
  1594. composition, i.e., creating classes that contain other 
  1595. classes as members; this technique might help you 
  1596. represent the button, bell and door objects inside the 
  1597. elevator as members of the elevator, for example. Also 
  1598. in Chapter 7, you will learn how to create and destroy 
  1599. objects dynamically with <b>new</b> and <b>delete</b>; this will help 
  1600. you create new person objects as new people arrive in 
  1601. the simulation and destroy these person objects as 
  1602. people leave the simulation (after getting off the 
  1603. elevator).<br>
  1604.  
  1605. </page>
  1606. <page>
  1607. 3. For the first version of your simulator, design only a 
  1608. simple, text-oriented output that displays a message for 
  1609. each significant event that occurs. Your messages might 
  1610. include strings such as: "Person 1 arrives on Floor 1," 
  1611. "Person 1 presses Button on Floor 1," "Elevator arrives 
  1612. on Floor 1," "Person 1 enters Elevator," etc. Note that 
  1613. we suggest you capitalize the words of each message 
  1614. that represent objects in your simulation. Note also that 
  1615. you may choose to defer this portion of the lab 
  1616. assignment until you have read Chapter 7.<br>
  1617. <spacer width=16 height=1>4. The more ambitious students will want to use an 
  1618. animated graphical output that shows the elevator 
  1619. moving up and down on the screen. <br>
  1620.  
  1621. </page>
  1622. </section>
  1623. <section type=Body name=Default title="6.20 Summary">
  1624. <page>
  1625. <font size=18 bold>6.20 Summary</font><hr>
  1626. <indent width=8 delay>*   Structures are aggregate data types built using data of 
  1627. other types. </indent>
  1628. <indent width=8 delay>*   The keyword <b>struct</b> introduces a structure definition. 
  1629. The body of a structure is delineated by braces (<b>{</b> and <b>}</b>). 
  1630. Every structure definition must end with a semicolon. </indent>
  1631. <indent width=8 delay>*   A structure tag name can be used to declare variables 
  1632. of a structure type.</indent>
  1633. <indent width=8 delay>*   Structure definitions do not reserve space in memory; 
  1634. they create new data types that are used to declare variables. </indent>
  1635. <indent width=8 delay>*  Members of a structure or a class are accessed using </indent>
  1636.  
  1637. </page>
  1638. <page>
  1639. <indent width=8 delay>*   the member access operators--the dot operator (<b>.</b>) and 
  1640. the arrow operator (<b>-></b>). The dot operator accesses a 
  1641. structure member via the object's variable name or a 
  1642. reference to the object. The arrow operator accesses a 
  1643. structure member via a pointer to the object. </indent>
  1644. <indent width=8 delay>*   Drawbacks to creating new data types with <b>struct</b>s 
  1645. are the possibility of having uninitialized data; 
  1646. improper initialization; all programs using a <b>struct</b> 
  1647. must be changed if the <b>struct</b> implementation changes; 
  1648. and no protection is provided to ensure that data is kept 
  1649. in a consistent state with proper data values. </indent>
  1650. <indent width=8 delay>*  Classes enable the programmer to model objects with 
  1651. attributes and behaviors. Class types can be defined in </indent>
  1652.  
  1653. </page>
  1654. <page>
  1655. <indent width=8 delay>*   C++ using the keywords <b>class</b> and <b>struct</b>, but keyword 
  1656. <b>class</b> is normally used for this purpose.</indent>
  1657. <indent width=8 delay>*   The class name can be used to declare objects of that 
  1658. class. </indent>
  1659. <indent width=8 delay>*   Class definitions begin with the keyword <b>class</b>. The 
  1660. body of the class definition is delineated with braces (<b>{</b> 
  1661. and <b>}</b>). Class definitions terminate with a semicolon. </indent>
  1662. <indent width=8 delay>*   Any data member or member function declared after 
  1663. <b>public:</b> in a class is visible to any function with access 
  1664. to an object of the class.</indent>
  1665. <indent width=8 delay>*   Any data member or member function declared after 
  1666. <b>private: </b>is only visible to <b>friend</b>s and other members 
  1667. of the class. </indent>
  1668.  
  1669. </page>
  1670. <page>
  1671. <indent width=8 delay>*   Member access specifiers always end with a colon (<b>:</b>) 
  1672. and can appear multiple times and in any order in a 
  1673. class definition.</indent>
  1674. <indent width=8 delay>*   Private data are not accessible from outside the class. </indent>
  1675. <indent width=8 delay>*   The implementation of a class should be hidden from 
  1676. its clients. </indent>
  1677. <indent width=8 delay>*   A constructor is a special member function with the 
  1678. same name as the class that is used to initialize the 
  1679. members of a class object. A class's constructor is 
  1680. called when an object of that class is instantiated. </indent>
  1681. <indent width=8 delay>*   The function with the same name as the class but preceded with a tilde character (<b>~</b>) is called a destructor. </indent>
  1682. <indent width=8 delay>*  The set of <b>public</b> member functions of a class is </indent>
  1683.  
  1684. </page>
  1685. <page>
  1686. <indent width=8 delay>*   called the class's interface or <b>public</b> interface.</indent>
  1687. <indent width=8 delay>*   When a member function is defined outside the class 
  1688. definition, the function name is preceded by the class 
  1689. name and the binary scope resolution operator (<b>::</b>). </indent>
  1690. <indent width=8 delay>*   Member functions defined using the scope resolution 
  1691. operator outside a class definition are within that class's 
  1692. scope.</indent>
  1693. <indent width=8 delay>*   Member functions defined in a class definition are 
  1694. automatically inlined. The compiler reserves the right 
  1695. not to inline any function.</indent>
  1696. <indent width=8 delay>*  Calling member functions is more concise than calling functions in procedural programming because most 
  1697. data used by the member function is directly accessible </indent>
  1698.  
  1699. </page>
  1700. <page>
  1701. <indent width=8 delay>*   in the object.</indent>
  1702. <indent width=8 delay>*   Within a class's scope, class members may be referenced simply by their names. Outside a class's scope, 
  1703. class members are referenced through either an object 
  1704. name, a reference to an object, or a pointer to an object. </indent>
  1705. <indent width=8 delay>*   Member selection operators <b>.</b> and <b>-></b> are used to 
  1706. access class members.</indent>
  1707. <indent width=8 delay>*   A fundamental principle of good software engineering is to separate interface from implementation. </indent>
  1708. <indent width=8 delay>*   Class definitions are normally placed in header files 
  1709. and member function definitions are normally placed in 
  1710. source-code files of the same base name.</indent>
  1711. <indent width=8 delay>*  The default access mode for classes is <b>private</b> so all </indent>
  1712.  
  1713. </page>
  1714. <page>
  1715. <indent width=8 delay>*   members after the class header and before the first 
  1716. member access specifier are considered to be <b>private</b>. </indent>
  1717. <indent width=8 delay>*   A class's <b>public</b> members present a view of the services the class provides to the clients of the class. </indent>
  1718. <indent width=8 delay>*   Access to a class's <b>private</b> data can be carefully controlled by the use of member functions called access 
  1719. functions. If a class wants to allow clients to read <b>private</b> data, the class can provide a <i>get</i> function. To 
  1720. enable clients to modify <b>private</b> data, the class can provide a <i>set</i> function. </indent>
  1721. <indent width=8 delay>*  Data members of a class are normally made <b>private</b> 
  1722. and member functions of a class are normally made 
  1723. <b>public</b>. Some member functions may be <b>private</b> and </indent>
  1724.  
  1725. </page>
  1726. <page>
  1727. <indent width=8 delay>*   serve as utility functions to the other functions of the 
  1728. class.</indent>
  1729. <indent width=8 delay>*   Data members of a class cannot be initialized in a 
  1730. class definition. They must be initialized in a constructor, or their values may be <i>set</i> after their object is created.</indent>
  1731. <indent width=8 delay>*   Constructors can be overloaded. </indent>
  1732. <indent width=8 delay>*   Once a class object is properly initialized, all member functions that manipulate the object should ensure 
  1733. that the object remains in a consistent state. </indent>
  1734. <indent width=8 delay>*   When an object of a class is declared, initializers can 
  1735. be provided. These initializers are passed to the class's 
  1736. constructor. </indent>
  1737.  
  1738. </page>
  1739. <page>
  1740. <indent width=8 delay>*   Constructors can specify default arguments. </indent>
  1741. <indent width=8 delay>*   Constructors may not specify return types, nor may 
  1742. they attempt to return values.</indent>
  1743. <indent width=8 delay>*   If no constructor is defined for a class, the compiler 
  1744. creates a default constructor. A default constructor supplied by the compiler does not perform any initialization, so when the object is created, it is not guaranteed 
  1745. to be in a consistent state.</indent>
  1746. <indent width=8 delay>*   The destructor of an automatic object is called when 
  1747. the object goes out of scope. The destructor itself does 
  1748. not actually destroy the object, but it does perform termination housekeeping before the system reclaims the 
  1749. object's storage. </indent>
  1750.  
  1751. </page>
  1752. <page>
  1753. <indent width=8 delay>*   Destructors do not receive parameters and do not 
  1754. return values. A class may have only one destructor 
  1755. (destructors cannot be overloaded).</indent>
  1756. <indent width=8 delay>*   The assignment operator (<b>=</b>) is used to assign an 
  1757. object to another object of the same type. Such assignment is normally performed by default memberwise 
  1758. copy. Memberwise copy is not ideal for all classes.</indent>
  1759.  
  1760. </page>
  1761. </section>
  1762. <section type=Popup name=Debug title="Testing">
  1763. <page>
  1764. The fact that member 
  1765. function calls generally 
  1766. take either no 
  1767. arguments or 
  1768. substantially fewer 
  1769. arguments than 
  1770. conventional function 
  1771. calls in non-object-
  1772. oriented languages 
  1773. reduces the likelihood <br>
  1774.  
  1775. </page>
  1776. <page>
  1777. of passing the wrong 
  1778. arguments, the wrong 
  1779. types of arguments, 
  1780. and/or the wrong 
  1781. number of arguments.<br>
  1782. <br>
  1783.  
  1784. </page>
  1785. <page>
  1786. Use <b>#ifndef</b>, <b>#define</b> 
  1787. and <b>#endif 
  1788. </b>preprocessor directives 
  1789. to prevent header files 
  1790. from being included 
  1791. more than once in a 
  1792. program.<br>
  1793. <br>
  1794.  
  1795. </page>
  1796. <page>
  1797. Making the data 
  1798. members of a class 
  1799. <b>private</b> and the 
  1800. member functions of 
  1801. the class <b>public</b> 
  1802. facilitates debugging 
  1803. because problems with 
  1804. data manipulations are 
  1805. localized to either the 
  1806. class's member <br>
  1807.  
  1808. </page>
  1809. <page>
  1810. functions or the <b>friend</b>s 
  1811. of the class.<br>
  1812. <br>
  1813.  
  1814. </page>
  1815. <page>
  1816. Every member function 
  1817. (and <b>friend</b>) that 
  1818. modifies the <b>private</b> 
  1819. data members of an 
  1820. object should ensure 
  1821. that the data remains in 
  1822. a consistent state.<br>
  1823. <br>
  1824.  
  1825. </page>
  1826. <page>
  1827. The benefits of data 
  1828. integrity are not 
  1829. automatic simply 
  1830. because data members 
  1831. are made <b>private</b>--the 
  1832. programmer must 
  1833. provide the validity 
  1834. checking. C++ does, 
  1835. however, provide a 
  1836. framework in which <br>
  1837.  
  1838. </page>
  1839. <page>
  1840. programmers can 
  1841. design better programs 
  1842. in a convenient manner. <br>
  1843. <br>
  1844.  
  1845. </page>
  1846. </section>
  1847. <section type=Popup name=Terminology title="Terminology">
  1848. <page>
  1849. <font size=14>
  1850. A<br>
  1851. abstract data type 
  1852. <a href="%s4p4"><img src=iicons/bullbib.gif></a>
  1853. <br>
  1854. access function 
  1855. <a href="%s8p3"><img src=iicons/bullbib.gif></a>
  1856. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  1857. <br>
  1858. arrow member selection 
  1859. operator (<b>-></b>) 
  1860. <a href="%s6p2"><img src=iicons/bullbib.gif></a>
  1861. <br>
  1862. attribute 
  1863. <a href="%s1p1"><img src=iicons/bullbib.gif></a>
  1864. <a href="%s1p1"><img src=iicons/bullbib.gif></a>
  1865. <a href="%s5p0"><img src=iicons/bullbib.gif></a>
  1866. <br>
  1867. B<br>
  1868. behavior 
  1869. <a href="%s1p1"><img src=iicons/bullbib.gif></a>
  1870. <a href="%s1p1"><img src=iicons/bullbib.gif></a>
  1871. <a href="%s5p0"><img src=iicons/bullbib.gif></a>
  1872. <a href="%s5p10"><img src=iicons/bullbib.gif></a>
  1873.  
  1874. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  1875. <br>
  1876. binary scope resolution 
  1877. operator (<b>::</b>) 
  1878. <a href="%s5p11"><img src=iicons/bullbib.gif></a>
  1879. <br>
  1880. C<br>
  1881. <b>class</b> 
  1882. <a href="%s5p0"><img src=iicons/bullbib.gif></a>
  1883. <br>
  1884. class definition 
  1885. <a href="%s5p2"><img src=iicons/bullbib.gif></a>
  1886. <a href="%s19p0"><img src=iicons/bullbib.gif></a>
  1887. <br>
  1888. </font>
  1889.  
  1890. </page>
  1891. <page>
  1892. <font size=14>
  1893. class scope 
  1894. <a href="%s5p12"><img src=iicons/bullbib.gif></a>
  1895. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  1896. <br>
  1897. client of a class 
  1898. <a href="%s14p3"><img src=iicons/bullbib.gif></a>
  1899. <br>
  1900. consistent state 
  1901. <a href="%s5p8"><img src=iicons/bullbib.gif></a>
  1902. <a href="%s11p4"><img src=iicons/bullbib.gif></a>
  1903.  
  1904. <a href="^Debug::c:s0p5"><img src=iicons/bullbib.gif></a>
  1905. <a href="%s14p4"><img src=iicons/bullbib.gif></a>
  1906. <a href="^Practice::c:s0p9"><img src=iicons/bullbib.gif></a>
  1907. <br>
  1908. constructors 
  1909. <a href="%s10p0"><img src=iicons/bullbib.gif></a>
  1910. <br>
  1911. D<br>
  1912. data member 
  1913. <a href="%s1p4"><img src=iicons/bullbib.gif></a>
  1914. <a href="%s5p0"><img src=iicons/bullbib.gif></a>
  1915. <a href="%s5p2"><img src=iicons/bullbib.gif></a>
  1916.  
  1917. <a href="%s5p11"><img src=iicons/bullbib.gif></a>
  1918. <br>
  1919. default constructor 
  1920. <a href="%s11p1"><img src=iicons/bullbib.gif></a>
  1921. <br>
  1922. destructor 
  1923. <a href="%s5p9"><img src=iicons/bullbib.gif></a>
  1924. <a href="%s12p0"><img src=iicons/bullbib.gif></a>
  1925. <a href="%s12p0"><img src=iicons/bullbib.gif></a>
  1926. <br>
  1927. dot member selection 
  1928. operator (<b>.</b>) 
  1929. <a href="%s6p2"><img src=iicons/bullbib.gif></a>
  1930. <br>
  1931. E<br>
  1932. encapsulation 
  1933. <a href="^Engineer::c:s0p13"><img src=iicons/bullbib.gif></a>
  1934. <a href="^Practice::c:s0p10"><img src=iicons/bullbib.gif></a>
  1935. <br>
  1936. </font>
  1937.  
  1938. </page>
  1939. <page>
  1940. <font size=14>
  1941. F<br>
  1942. file scope 
  1943. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  1944. <br>
  1945. G<br>
  1946. get function 
  1947. <a href="%s8p4"><img src=iicons/bullbib.gif></a>
  1948. <a href="%s14p3"><img src=iicons/bullbib.gif></a>
  1949.  
  1950. <a href="^Engineer::c:s0p40"><img src=iicons/bullbib.gif></a>
  1951. <br>
  1952. global object 
  1953. <a href="%s13p1"><img src=iicons/bullbib.gif></a>
  1954. <br>
  1955. H<br>
  1956. header file 
  1957. <a href="%s7p4"><img src=iicons/bullbib.gif></a>
  1958. <a href="^Engineer::c:s0p17"><img src=iicons/bullbib.gif></a>
  1959. <a href="%s19p0"><img src=iicons/bullbib.gif></a>
  1960. <br>
  1961. helper function 
  1962. <a href="%s9p1"><img src=iicons/bullbib.gif></a>
  1963. <br>
  1964. I<br>
  1965. implementation of a 
  1966. class 
  1967. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  1968. <a href="^Engineer::c:s0p20"><img src=iicons/bullbib.gif></a>
  1969. <br>
  1970. information hiding 
  1971. <a href="%s1p2"><img src=iicons/bullbib.gif></a>
  1972.  
  1973. <a href="%s5p8"><img src=iicons/bullbib.gif></a>
  1974. <a href="^Engineer::c:s0p27"><img src=iicons/bullbib.gif></a>
  1975. <br>
  1976. </font>
  1977.  
  1978. </page>
  1979. <page>
  1980. <font size=14>
  1981. initializing class objects 
  1982.  
  1983. <a href="%s10p0"><img src=iicons/bullbib.gif></a>
  1984. <a href="^Engineer::c:s0p30"><img src=iicons/bullbib.gif></a>
  1985. <br>
  1986. <b>inline</b> 
  1987. <a href="%s5p13"><img src=iicons/bullbib.gif></a>
  1988. <br>
  1989. interface of class 
  1990. <a href="%s5p3"><img src=iicons/bullbib.gif></a>
  1991. <br>
  1992. M<br>
  1993. member access specifier 
  1994.  
  1995. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  1996. <br>
  1997. member function 
  1998. <a href="%s1p4"><img src=iicons/bullbib.gif></a>
  1999. <a href="%s5p0"><img src=iicons/bullbib.gif></a>
  2000.  
  2001. <a href="%s5p2"><img src=iicons/bullbib.gif></a>
  2002. <a href="%s5p11"><img src=iicons/bullbib.gif></a>
  2003. <br>
  2004. member selection 
  2005. operators 
  2006. <a href="%s6p3"><img src=iicons/bullbib.gif></a>
  2007. <br>
  2008. memberwise copy 
  2009. <a href="%s16p0"><img src=iicons/bullbib.gif></a>
  2010. <br>
  2011. message sent to an object 
  2012.  
  2013. <a href="%s5p0"><img src=iicons/bullbib.gif></a>
  2014. <br>
  2015. </font>
  2016.  
  2017. </page>
  2018. <page>
  2019. <font size=14>
  2020. O<br>
  2021. object 
  2022. <a href="%s1p5"><img src=iicons/bullbib.gif></a>
  2023. <br>
  2024. object-oriented 
  2025. programming (OOP) 
  2026. <a href="%s5p3"><img src=iicons/bullbib.gif></a>
  2027. <br>
  2028. P<br>
  2029. predicate function 
  2030. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  2031. <br>
  2032. principle of least 
  2033. privilege 
  2034. <a href="^Engineer::c:s0p22"><img src=iicons/bullbib.gif></a>
  2035. <a href="^Engineer::c:s0p27"><img src=iicons/bullbib.gif></a>
  2036. <br>
  2037. <b>private</b> 
  2038. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  2039. <a href="%s5p2"><img src=iicons/bullbib.gif></a>
  2040. <a href="%s5p6"><img src=iicons/bullbib.gif></a>
  2041. <a href="^Engineer::c:s0p29"><img src=iicons/bullbib.gif></a>
  2042. <br>
  2043. procedural programming 
  2044. languages 
  2045. <a href="%s1p3"><img src=iicons/bullbib.gif></a>
  2046. <br>
  2047. programmer-defined 
  2048. type 
  2049. <a href="%s1p4"><img src=iicons/bullbib.gif></a>
  2050. <br>
  2051. <b>protected</b> 
  2052. <a href="%s5p3"><img src=iicons/bullbib.gif></a>
  2053. <a href="%s6p4"><img src=iicons/bullbib.gif></a>
  2054. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  2055. <br>
  2056. </font>
  2057.  
  2058. </page>
  2059. <page>
  2060. <font size=14>
  2061. proxy class 
  2062. <a href="%s7p1"><img src=iicons/bullbib.gif></a>
  2063. <br>
  2064. <b>public</b> interface 
  2065. <a href="%s5p10"><img src=iicons/bullbib.gif></a>
  2066. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  2067.  
  2068. <a href="^Engineer::c:s0p19"><img src=iicons/bullbib.gif></a>
  2069. <a href="^Practice::c:s0p3"><img src=iicons/bullbib.gif></a>
  2070. <br>
  2071. Q<br>
  2072. query function 
  2073. <a href="%s14p1"><img src=iicons/bullbib.gif></a>
  2074. <br>
  2075. R<br>
  2076. rapid applications 
  2077. development (RAD) 
  2078. <a href="%s17p1"><img src=iicons/bullbib.gif></a>
  2079. <br>
  2080. reuse 
  2081. <a href="%s5p15"><img src=iicons/bullbib.gif></a>
  2082. <br>
  2083. S<br>
  2084. scope resolution operator 
  2085. (<b>::</b>) 
  2086. <a href="%s3p2"><img src=iicons/bullbib.gif></a>
  2087. <br>
  2088. self-referential structure 
  2089.  
  2090. <a href="%s2p2"><img src=iicons/bullbib.gif></a>
  2091. <br>
  2092. </font>
  2093.  
  2094. </page>
  2095. <page>
  2096. <font size=14>
  2097. service 
  2098. <a href="%s5p10"><img src=iicons/bullbib.gif></a>
  2099. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  2100. <br>
  2101. set function 
  2102. <a href="%s8p4"><img src=iicons/bullbib.gif></a>
  2103. <a href="%s14p3"><img src=iicons/bullbib.gif></a>
  2104.  
  2105. <a href="%s14p5"><img src=iicons/bullbib.gif></a>
  2106. <a href="^Engineer::c:s0p40"><img src=iicons/bullbib.gif></a>
  2107. <br>
  2108. software reuse 
  2109. <a href="%s5p15"><img src=iicons/bullbib.gif></a>
  2110. <br>
  2111. source-code file 
  2112. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  2113. <br>
  2114. <b>static</b> local object 
  2115. <a href="%s13p2"><img src=iicons/bullbib.gif></a>
  2116.  
  2117. <a href="%s13p3"><img src=iicons/bullbib.gif></a>
  2118. <a href="%s13p4"><img src=iicons/bullbib.gif></a>
  2119. <br>
  2120. structure 
  2121. <a href="%s1p5"><img src=iicons/bullbib.gif></a>
  2122. <br>
  2123. T<br>
  2124. tilde character (<b>~</b>) 
  2125. <a href="%s5p9"><img src=iicons/bullbib.gif></a>
  2126.  
  2127. <a href="%s12p0"><img src=iicons/bullbib.gif></a>
  2128. <br>
  2129. U<br>
  2130. user-defined type 
  2131. <a href="%s1p4"><img src=iicons/bullbib.gif></a>
  2132. <br>
  2133. utility function 
  2134. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  2135. <a href="%s9p2"><img src=iicons/bullbib.gif></a>
  2136. <br>
  2137. </font>
  2138.  
  2139. </page>
  2140. </section>
  2141. <section type=Popup name=Illustration title="Illustrations">
  2142. <page>
  2143. <a href="^Code::c:s0p0">Fig. 6.1</a>  Creating a structure, setting its members, and printing the structure.<br>
  2144. <a href="^Code::c:s0p1">Fig. 6.2</a>  Simple definition of <b>class</b> <b>Time</b>.<br>
  2145. <a href="^Code::c:s0p2">Fig. 6.3</a>  Abstract data type <b>Time</b> implementation as a class.<br>
  2146. <a href="^Code::c:s0p3">Fig. 6.4</a>  Accessing an object's data members and member functions through each type of 
  2147. object handle--through the object's name, through a reference, and through a pointer to the 
  2148. object.<br>
  2149. <a href="^Code::c:s0p4">Fig. 6.5</a>  Separating <b>Time</b> class interface and implementation.<br>
  2150. <a href="^Code::c:s0p5">Fig. 6.6</a>  Erroneous attempt to access <b>private</b> members of a class.<br>
  2151. <a href="^Code::c:s0p6">Fig. 6.7</a>  Using a utility function.<br>
  2152. <a href="^Code::c:s0p7">Fig. 6.8</a>  Using a constructor with default arguments.<br>
  2153. <a href="^Code::c:s0p8">Fig. 6.9</a>  Demonstrating the order in which constructors and destructors are called.<br>
  2154. <a href="^Code::c:s0p9">Fig. 6.10</a>  Using set and get functions.<br>
  2155. <a href="^Code::c:s0p10">Fig. 6.11</a>  Returning a reference to a private data member.<br>
  2156. <a href="^Code::c:s0p11">Fig. 6.12</a>  Assigning one object to another with default memberwise copy.<br>
  2157. <br>
  2158.  
  2159. </page>
  2160. </section>
  2161. <section type=Popup name=Exercises title="Exercises">
  2162. <page pagename="Exercise 6.1">
  2163. <b>Exercise 6.1</b><br>
  2164. Fill in the blanks in each of the following:<br>
  2165. a)  The keyword _______ introduces a structure definition.<br>
  2166. b)  Class members are accessed via the _______ operator in conjunction with 
  2167. the name of an object of the class or via the ________operator in conjunction 
  2168. with a pointer to an object of the class.<br>
  2169. c)  Members of a class specified as _______ are accessible only to member 
  2170. functions of the class and <b>friend</b>s of the class.<br>
  2171. d)  A _______ is a special member function used to initialize the data members 
  2172. of a class.<br>
  2173. e)  The default access for members of a class is _______ .<br>
  2174. <foreign  name="answers" url="^Answers::c:s0p0">
  2175.  
  2176. </page>
  2177. <page pagename="Exercise 6.1">
  2178. f)  A ________function is used to assign values to <b>private</b> data members of a 
  2179. class.<br>
  2180. g)  ________ can be used to assign an object of a class to another object of 
  2181. the same class.<br>
  2182. h)  Member functions of a class are normally made _______ and data members 
  2183. of a class are normally made ________.<br>
  2184. i)  A _______ function is used to retrieve values of <b>private</b> data of a class.<br>
  2185. j)  The set of <b>public</b> member functions of a class is referred to as the class's 
  2186. _____.<br>
  2187. k)  A class implementation is said to be hidden from its clients or ________.<br>
  2188. l)  The keywords _______ and _______ can be used to introduce a class 
  2189. definition.<br>
  2190. <foreign  name="answers" url="^Answers::c:s0p0">
  2191.  
  2192. </page>
  2193. <page pagename="Exercise 6.1">
  2194. m)  Members of a class specified as _______ are accessible anywhere an 
  2195. object of the class is in scope.<br>
  2196. <foreign  name="answers" url="^Answers::c:s0p0">
  2197. <br>
  2198.  
  2199. </page>
  2200. <page pagename="Exercise 6.2">
  2201. <b>Exercise 6.2</b><br>
  2202. Find the error(s) in each of the following and explain how to correct it.  <br>
  2203. a)  Assume the following prototype is declared in class <b>Time</b>.<br>
  2204. <font size=2><br></font><font size=11><pre>
  2205. void ~Time( int );<p>
  2206. </pre></font>
  2207. b)  The following is a partial definition of class <b>Time</b>.<br>
  2208. <font size=2><br></font><font size=11><pre>
  2209. class Time {<p>
  2210. public:<p>
  2211.    // function prototypes<p>
  2212. private:<p>
  2213.    int hour = 0;<p>
  2214.  int minute = 0;<p>
  2215. </pre></font>
  2216. <foreign  name="answers" url="^Answers::c:s0p2">
  2217.  
  2218. </page>
  2219. <page pagename="Exercise 6.2">
  2220. <font size=2><br></font><font size=11><pre>
  2221.      int second = 0;<p>
  2222. </pre></font>
  2223. <font size=2><br></font><font size=11><pre>
  2224. };<p>
  2225. </pre></font>
  2226. c)  Assume the following prototype is declared in class <b>Employee</b>.<br>
  2227. <font size=2><br></font><font size=11><pre>
  2228. int Employee( const char *, const char * );<p>
  2229. </pre></font>
  2230. <foreign  name="answers" url="^Answers::c:s0p2">
  2231. <br>
  2232.  
  2233. </page>
  2234. <page pagename="Exercise 6.3">
  2235. <b>Exercise 6.3</b><br>
  2236. What is the purpose of the scope resolution operator?<br>
  2237. <br>
  2238.  
  2239. </page>
  2240. <page pagename="Exercise 6.4">
  2241. <b>Exercise 6.4</b><br>
  2242. Compare and contrast the notions of <b>struct</b> and <b>class</b> in C++.<br>
  2243. <br>
  2244. <br>
  2245.  
  2246. </page>
  2247. <page pagename="Exercise 6.5">
  2248. <b>Exercise 6.5</b><br>
  2249. Provide a constructor that is capable of using the current time from the <b>time()</b> 
  2250. function--declared in the C Standard Library header <b>time.h</b>--to initialize an 
  2251. object of the <b>Time</b> class.<br>
  2252. <foreign  name="answers" url="^Answers::c:s0p3">
  2253.  
  2254. </page>
  2255. <page pagename="Exercise 6.6">
  2256. <b>Exercise 6.6</b><br>
  2257. Create a class called <b>Complex</b> for performing arithmetic with complex numbers. 
  2258. Write a driver program to test your class. <br>
  2259. Complex numbers have the form <br>
  2260. <font size=2><br></font><font size=11><pre>
  2261. realPart + imaginaryPart * i<p>
  2262. </pre></font>
  2263. where <i>i</i> is <img src="graphics/ch06/ex06006.gif" ><br>
  2264. Use floating-point variables to represent the <b>private</b> data of the class. Provide a 
  2265. constructor function that enables an object of this class to be initialized when it 
  2266. is declared. The constructor should contain default values in case no initializers 
  2267. are provided. Provide <b>public</b> member functions for each of the following:<br>
  2268.  
  2269. </page>
  2270. <page pagename="Exercise 6.6">
  2271. a)  Addition of two <b>Complex</b> numbers: The real parts are added together and the 
  2272. imaginary parts are added together.<br>
  2273. b)  Subtraction of two <b>Complex</b> numbers: The real part of the right operand is 
  2274. subtracted from the real part of the left operand and the imaginary part of the 
  2275. right operand is subtracted from the imaginary part of the left operand.<br>
  2276. c)  Printing <b>Complex</b> numbers in the form <b>(a, b)</b> where <b>a</b> is the real part and <b>b</b> is 
  2277. the imaginary part.<br>
  2278. <br>
  2279. <br>
  2280.  
  2281. </page>
  2282. <page pagename="Exercise 6.7">
  2283. <b>Exercise 6.7</b><br>
  2284. Create a class called <b>Rational</b> for performing arithmetic with fractions. Write a 
  2285. driver program to test your class.<br>
  2286. Use integer variables to represent the <b>private</b> data of the class--the numerator 
  2287. and the denominator. Provide a constructor function that enables an object of 
  2288. this class to be initialized when it is declared. The constructor should contain 
  2289. default values in case no initializers are provided and should store the fraction in 
  2290. reduced form (i.e., the fraction <img src="graphics/ch06/ex06007.gif" ><br>
  2291. would be stored in the object as 1 in the numerator and 2 in the denominator). 
  2292. Provide <b>public</b> member functions for each of the following:<br>
  2293. <foreign  name="answers" url="^Answers::c:s0p4">
  2294.  
  2295. </page>
  2296. <page pagename="Exercise 6.7">
  2297. a)  Addition of two <b>Rational</b> numbers. The result should be stored in reduced 
  2298. form.<br>
  2299. b)  Subtraction of two <b>Rational</b> numbers. The result should be stored in reduced 
  2300. form.<br>
  2301. c)  Multiplication of two <b>Rational</b> numbers. The result should be stored in 
  2302. reduced form.<br>
  2303. d)  Division of two <b>Rational</b> numbers. The result should be stored in reduced 
  2304. form.<br>
  2305. e)  Printing <b>Rational</b> numbers in the form <b>a/b</b> where <b>a</b> is the numerator and <b>b</b> is 
  2306. the denominator.<br>
  2307. f)  Printing <b>Rational</b> numbers in floating-point format.<br>
  2308. <foreign  name="answers" url="^Answers::c:s0p4">
  2309.  
  2310. </page>
  2311. <page pagename="Exercise 6.8">
  2312. <b>Exercise 6.8</b><br>
  2313. Modify the <b>Time</b> class of<a href="^Code::c:s0p9"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.10 </a>to include a <b>tick</b> member function that 
  2314. increments the time stored in a <b>Time</b> object by one second. The <b>Time</b> object 
  2315. should always remain in a consistent state. Write a driver program that tests the 
  2316. <b>tick</b> member function in a loop that prints the time in standard format during 
  2317. each iteration of the loop to illustrate that the <b>tick</b> member function works 
  2318. correctly. Be sure to test the following cases:<br>
  2319. a)  Incrementing into the next minute.<br>
  2320. b)  Incrementing into the next hour.<br>
  2321. c)  Incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).<br>
  2322. <foreign  name="answers" url="^Answers::c:s0p5">
  2323.  
  2324. </page>
  2325. <page pagename="Exercise 6.9">
  2326. <b>Exercise 6.9</b><br>
  2327. Modify the <b>Date</b> class of <a href="^Code::c:s0p11"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.12</a> to perform error checking on the initializer 
  2328. values for data members <b>month</b>, <b>day</b>, and <b>year</b>. Also, provide a member 
  2329. function <b>nextDay</b> to increment the day by one. The <b>Date</b> object should always 
  2330. remain in a consistent state. Write a driver program that tests the <b>nextDay</b> 
  2331. function in a loop that prints the date during each iteration of the loop to 
  2332. illustrate that the <b>nextDay</b> function works correctly. Be sure to test the following 
  2333. cases:<br>
  2334. a)  Incrementing into the next month.<br>
  2335. b)  Incrementing into the next year.<br>
  2336. <br>
  2337. <br>
  2338.  
  2339. </page>
  2340. <page pagename="Exercise 6.10">
  2341. <b>Exercise 6.10</b><br>
  2342. Combine the modified <b>Time</b> class of<a href="^Exercises::c:s0p12"> <img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 6.8</a> and the modified <b>Date</b> class 
  2343. of<a href="^Exercises::c:s0p13"> <img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 6.9</a> into one class called <b>DateAndTime</b> (in Chapter 9 we will 
  2344. discuss inheritance which will enable us to accomplish this task quickly without 
  2345. modifying the existing class definitions). Modify the <b>tick</b> function to call the 
  2346. <b>nextDay</b> function if the time is incremented into the next day. Modify function 
  2347. <b>printStandard</b> and <b>printMilitary</b> to output the date in addition to the time. 
  2348. Write a driver program to test the new class <b>DateAndTime</b>. Specifically test 
  2349. incrementing the time into the next day.<br>
  2350. <br>
  2351. <br>
  2352.  
  2353. </page>
  2354. <page pagename="Exercise 6.11">
  2355. <b>Exercise 6.11</b><br>
  2356. Modify the <i>set</i> functions in the program of<a href="^Code::c:s0p9"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 6.10</a> to return appropriate error 
  2357. values if an attempt is made to <i>set</i> a data member of an object of class <b>Time</b> to 
  2358. an invalid value. <br>
  2359. <br>
  2360. <br>
  2361.  
  2362. </page>
  2363. <page pagename="Exercise 6.12">
  2364. <b>Exercise 6.12</b><br>
  2365. Create a class <b>Rectangle</b>. The class has attributes <b>length</b> and <b>width</b>, each of 
  2366. which defaults to 1. It has member functions that calculate the <b>perimeter</b> and 
  2367. the <b>area</b> of the rectangle. It has <i>set</i> and <i>get</i> functions for both <b>length</b> and <b>width</b>. 
  2368. The <i>set</i> functions should verify that <b>length</b> and <b>width</b> are each floating-point 
  2369. numbers larger than 0.0 and less than 20.0. <br>
  2370. <foreign  name="answers" url="^Answers::c:s0p6">
  2371.  
  2372. </page>
  2373. <page pagename="Exercise 6.13">
  2374. <b>Exercise 6.13</b><br>
  2375. Create a more sophisticated <b>Rectangle</b> class than the one you created in<a href="$currentLink"> 
  2376. <img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 6.12</a>. This class stores only the Cartesian coordinates of the four 
  2377. corners of the rectangle. The constructor calls a <i>set</i> function that accepts four 
  2378. sets of coordinates and verifies that each of these is in the first quadrant with no 
  2379. single x or y coordinate larger than 20.0. The <i>set</i> function also verifies that the 
  2380. supplied coordinates do, in fact, specify a rectangle. Member functions calculate 
  2381. the <b>length</b>, <b>width</b>, <b>perimeter</b>, and <b>area</b>. The length is the larger of the two 
  2382. dimensions. Include a predicate function <b>square</b> that determines if the rectangle 
  2383. is a square. <br>
  2384. <br>
  2385. <br>
  2386.  
  2387. </page>
  2388. <page pagename="Exercise 6.14">
  2389. <b>Exercise 6.14</b><br>
  2390. Modify the <b>Rectangle</b> class of <a href="^Exercises::c:s0p17"><img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 6.13</a> to include a <b>draw</b> function that 
  2391. displays the rectangle inside a 25-by-25 box enclosing the portion of the first 
  2392. quadrant in which the rectangle resides. Include a <b>setFillCharacter</b> function to 
  2393. specify the character out of which the body of the rectangle will be drawn. 
  2394. Include a <b>setPerimeterCharacter</b> function to specify the character that will be 
  2395. used to draw the border of the rectangle. If you feel ambitious you might include 
  2396. functions to scale the size of the rectangle, rotate it, and move it around within 
  2397. the designated portion of the first quadrant.<br>
  2398. <br>
  2399. <br>
  2400.  
  2401. </page>
  2402. <page pagename="Exercise 6.15">
  2403. <b>Exercise 6.15</b><br>
  2404. Create a class <b>HugeInteger</b> that uses a 40-element array of digits to store 
  2405. integers as large as 40-digits each. Provide member functions 
  2406. <b>inputHugeInteger</b>, <b>outputHugeInteger</b>, <b>addHugeIntegers</b>, and 
  2407. <b>substractHugeIntegers</b>. For comparing <b>HugeInteger</b> objects provide functions 
  2408. <b>isEqualTo</b>, <b>isNotEqualTo</b>, <b>isGreaterThan</b>, <b>isLessThan</b>, 
  2409. <b>IsGreaterThanOrEqualTo</b>, and <b>isLessThanOrEqualTo</b>--each of these is a 
  2410. "predicate" function that simply returns <b>true</b> if the relationship holds between 
  2411. the two huge integers and returns <b>false</b> if the relationship does not hold. Provide 
  2412. a predicate function <b>isZero</b>. If you feel ambitious, also provide member 
  2413. functions <b>multiplyHugeIntegers</b>, <b>divideHugeIntegers</b>, and 
  2414. <b>modulusHugeIntegers</b>.<br>
  2415.  
  2416. </page>
  2417. <page pagename="Exercise 6.16">
  2418. <b>Exercise 6.16</b><br>
  2419. Create a class <b>TicTacToe</b> that will enable you to write a complete program to 
  2420. play the game of tic-tac-toe. The class contains as <b>private</b> data a 3-by-3 double 
  2421. array of integers. The constructor should initialize the empty board to all zeros. 
  2422. Allow two human players. Wherever the first player moves, place a 1 in the 
  2423. specified square; place a 2 wherever the second player moves. Each move must 
  2424. be to an empty square. After each move, determine if the game has been won or 
  2425. if the game is a draw. If you feel ambitious, modify your program so that the 
  2426. computer makes the moves for one of the players automatically. Also, allow the 
  2427. player to specify whether he or she wants to go first or second. If you feel 
  2428. exceptionally ambitious, develop a program that will play three-dimensional tic-
  2429. tac-toe on a 4-by-4-by-4 board (Caution: This is an extremely challenging 
  2430. project that could take many weeks of effort!).<br>
  2431. <foreign  name="answers" url="^Answers::c:s0p7">
  2432.  
  2433. </page>
  2434. </section>
  2435. <section type=Popup name=Objective title="Objectives">
  2436. <page>
  2437. <indent width=8 delay>*   To understand the software engineering concepts of encapsulation and 
  2438. data hiding.</indent>
  2439. <indent width=8 delay>*   To understand the notions of data abstraction and abstract data types 
  2440. (ADTs).</indent>
  2441. <indent width=8 delay>*   To be able to create C++ ADTs, namely classes.</indent>
  2442. <foreign  name="audio" url="~audio/Ch06/06obj.au">
  2443.  
  2444. </page>
  2445. <page>
  2446. <indent width=8 delay>*   To understand how to create, use, and destroy class objects.</indent>
  2447. <indent width=8 delay>*   To be able to control access to object data members and member functions.</indent>
  2448. <indent width=8 delay>*   To begin to appreciate the value of object orientation.</indent>
  2449. <br>
  2450.  
  2451. </page>
  2452. </section>
  2453. <section type=Popup name=Code title="Code Examples">
  2454. <page>
  2455. <font size=18>Figure 6.1  Creating a structure, setting its members, and printing the structure.</font><br>
  2456. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2457. <param  name="file" value="code/ch06/fig06_01.txt">
  2458. </applet><br>
  2459. <br>
  2460. <foreign  name="copy2disk" url="!jcpy Source/fig06_01.jar">
  2461. <foreign  name="audio" url="~audio/Ch06/06fig001.au">
  2462. <foreign  name="execute" url="!jarexe Run/fig06_01.jar">
  2463. <br>
  2464.  
  2465. </page>
  2466. <page>
  2467. <font size=18>Figure 6.2  Simple definition of <b>class</b> <b>Time</b>.</font><br>
  2468. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2469. <param  name="file" value="code/ch06/fig06_02.txt">
  2470. </applet><br>
  2471. <br>
  2472. <foreign  name="audio" url="~audio/Ch06/06fig002.au">
  2473. <br>
  2474.  
  2475. </page>
  2476. <page>
  2477. <font size=18>Figure 6.3  Abstract data type <b>Time</b> implementation as a class.</font><br>
  2478. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2479. <param  name="file" value="code/ch06/fig06_03.txt">
  2480. </applet><br>
  2481. <br>
  2482. <foreign  name="copy2disk" url="!jcpy Source/fig06_03.jar">
  2483. <foreign  name="audio" url="~audio/Ch06/06fig003.au">
  2484. <foreign  name="execute" url="!jarexe Run/fig06_03.jar">
  2485. <br>
  2486.  
  2487. </page>
  2488. <page>
  2489. <font size=18>Figure 6.4  Accessing an object's data members and member functions through each 
  2490. type of object handle--through the object's name, through a reference, and through 
  2491. a pointer to the object.</font><br>
  2492. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2493. <param  name="file" value="code/ch06/fig06_04.txt">
  2494. </applet><br>
  2495. <br>
  2496. <foreign  name="copy2disk" url="!jcpy Source/fig06_04.jar">
  2497. <foreign  name="audio" url="~audio/Ch06/06fig004.au">
  2498. <foreign  name="execute" url="!jarexe Run/fig06_04.jar">
  2499. <br>
  2500.  
  2501. </page>
  2502. <page>
  2503. <font size=18>Figure 6.5   Separating <b>Time</b> class interface and implementation.</font><br>
  2504. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2505. <param  name="file" value="code/ch06/fig06_05.txt">
  2506. </applet><br>
  2507. <br>
  2508. <foreign  name="copy2disk" url="!jcpy Source/fig06_05.jar">
  2509. <foreign  name="audio" url="~audio/Ch06/06fig005.au">
  2510. <foreign  name="execute" url="!jarexe Run/fig06_05.jar">
  2511. <br>
  2512.  
  2513. </page>
  2514. <page>
  2515. <font size=18>Figure 6.6  Erroneous attempt to access <b>private</b> members of a class.</font><br>
  2516. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2517. <param  name="file" value="code/ch06/fig06_06.txt">
  2518. </applet><br>
  2519. <br>
  2520. <foreign  name="copy2disk" url="!jcpy Source/fig06_06.jar">
  2521. <foreign  name="audio" url="~audio/Ch06/06fig006.au">
  2522. <br>
  2523.  
  2524. </page>
  2525. <page>
  2526. <font size=18>Figure 6.7  Using a utility function.</font><br>
  2527. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2528. <param  name="file" value="code/ch06/fig06_07.txt">
  2529. </applet><br>
  2530. <br>
  2531. <foreign  name="copy2disk" url="!jcpy Source/fig06_07.jar">
  2532. <foreign  name="audio" url="~audio/Ch06/06fig007.au">
  2533. <foreign  name="execute" url="!jarexe Run/fig06_07.jar">
  2534. <br>
  2535.  
  2536. </page>
  2537. <page>
  2538. <font size=18>Figure 6.8  Using a constructor with default arguments.</font><br>
  2539. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2540. <param  name="file" value="code/ch06/fig06_08.txt">
  2541. </applet><br>
  2542. <br>
  2543. <foreign  name="copy2disk" url="!jcpy Source/fig06_08.jar">
  2544. <foreign  name="audio" url="~audio/Ch06/06fig008.au">
  2545. <foreign  name="execute" url="!jarexe Run/fig06_08.jar">
  2546. <br>
  2547.  
  2548. </page>
  2549. <page>
  2550. <font size=18>Figure 6.9  Demonstrating the order in which constructors and destructors are 
  2551. called.</font><br>
  2552. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2553. <param  name="file" value="code/ch06/fig06_09.txt">
  2554. </applet><br>
  2555. <br>
  2556. <foreign  name="copy2disk" url="!jcpy Source/fig06_09.jar">
  2557. <foreign  name="audio" url="~audio/Ch06/06fig009.au">
  2558. <foreign  name="execute" url="!jarexe Run/fig06_09.jar">
  2559. <br>
  2560.  
  2561. </page>
  2562. <page>
  2563. <font size=18>Figure 6.10  Using set and get functions.</font><br>
  2564. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2565. <param  name="file" value="code/ch06/fig06_10.txt">
  2566. </applet><br>
  2567. <br>
  2568. <foreign  name="copy2disk" url="!jcpy Source/fig06_10.jar">
  2569. <foreign  name="audio" url="~audio/Ch06/06fig010.au">
  2570. <foreign  name="execute" url="!jarexe Run/fig06_10.jar">
  2571. <br>
  2572.  
  2573. </page>
  2574. <page>
  2575. <font size=18>Figure 6.11  Returning a reference to a private data member.</font><br>
  2576. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2577. <param  name="file" value="code/ch06/fig06_11.txt">
  2578. </applet><br>
  2579. <br>
  2580. <foreign  name="copy2disk" url="!jcpy Source/fig06_11.jar">
  2581. <foreign  name="audio" url="~audio/Ch06/06fig011.au">
  2582. <foreign  name="execute" url="!jarexe Run/fig06_11.jar">
  2583. <br>
  2584.  
  2585. </page>
  2586. <page>
  2587. <font size=18>Figure 6.12  Assigning one object to another with default memberwise copy.</font><br>
  2588. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2589. <param  name="file" value="code/ch06/fig06_12.txt">
  2590. </applet><br>
  2591. <br>
  2592. <foreign  name="copy2disk" url="!jcpy Source/fig06_12.jar">
  2593. <foreign  name="audio" url="~audio/Ch06/06fig012.au">
  2594. <foreign  name="execute" url="!jarexe Run/fig06_12.jar">
  2595. <br>
  2596.  
  2597. </page>
  2598. </section>
  2599. <section type=Popup name=Perform title="Performance">
  2600. <page>
  2601. Structures are 
  2602. ordinarily passed call-
  2603. by-value. To avoid the 
  2604. overhead of copying a 
  2605. structure, pass the 
  2606. structure call-by-
  2607. reference.<br>
  2608. <br>
  2609.  
  2610. </page>
  2611. <page>
  2612. Defining a small 
  2613. member function inside 
  2614. the class definition 
  2615. automatically inlines 
  2616. the member function (if 
  2617. the compiler chooses to 
  2618. do so). This can 
  2619. improve performance, 
  2620. but it does not promote 
  2621. the best software <br>
  2622.  
  2623. </page>
  2624. <page>
  2625. engineering because 
  2626. clients of the class will 
  2627. be able to see the 
  2628. implementation of the 
  2629. function.<br>
  2630. <br>
  2631.  
  2632. </page>
  2633. <page>
  2634. Actually objects 
  2635. contain only data, so 
  2636. objects are much 
  2637. smaller than if they also 
  2638. contained functions. 
  2639. Applying operator 
  2640. <b>sizeof</b> to a class name 
  2641. or to an object of that 
  2642. class will report only 
  2643. the size of the class's <br>
  2644.  
  2645. </page>
  2646. <page>
  2647. data. The compiler 
  2648. creates one copy (only) 
  2649. of the class's member 
  2650. functions separate from 
  2651. all objects of the class. 
  2652. All objects of the class 
  2653. share this one copy of 
  2654. the member functions. 
  2655. Each object, of course, 
  2656. needs its own copy of <br>
  2657.  
  2658. </page>
  2659. <page>
  2660. the class's data because 
  2661. this data can vary 
  2662. among the objects. The 
  2663. function code is 
  2664. nonmodifiable (also 
  2665. called reentrant code or 
  2666. pure procedure) and 
  2667. hence can be shared 
  2668. among all objects of 
  2669. one class.<br>
  2670.  
  2671. </page>
  2672. <page>
  2673. Passing an object call-
  2674. by-value is good from a 
  2675. security standpoint 
  2676. because the called 
  2677. function has no access 
  2678. to the original object, 
  2679. but call-by-value can 
  2680. degrade performance 
  2681. when making a copy of 
  2682. a large object. An <br>
  2683.  
  2684. </page>
  2685. <page>
  2686. object can be passed 
  2687. call-by-reference by 
  2688. passing either a pointer 
  2689. or a reference to the 
  2690. object. Call-by-
  2691. reference offers good 
  2692. performance but is 
  2693. weaker from a security 
  2694. standpoint because the 
  2695. called function is given <br>
  2696.  
  2697. </page>
  2698. <page>
  2699. access to the original 
  2700. object. Call-by-<b>const</b>-
  2701. reference is a safe, 
  2702. good-performing 
  2703. alternative.<br>
  2704. <br>
  2705.  
  2706. </page>
  2707. </section>
  2708. <section type=Popup name=Practice title="Good Practices">
  2709. <page>
  2710. Use each member 
  2711. access specifier only 
  2712. once in a class 
  2713. definition for clarity 
  2714. and readability. Place 
  2715. <b>public</b> members first 
  2716. where they are easy to 
  2717. locate.<br>
  2718. <br>
  2719.  
  2720. </page>
  2721. <page>
  2722. Use the name of the 
  2723. header file with the 
  2724. period replaced by an 
  2725. underscore in the 
  2726. <b>#ifndef </b>and <b>#define</b> 
  2727. preprocessor directives 
  2728. of a header file.<br>
  2729. <br>
  2730.  
  2731. </page>
  2732. <page>
  2733. Despite the fact that the 
  2734. <b>public</b> and <b>private</b> 
  2735. labels may be repeated 
  2736. and intermixed, list all 
  2737. the <b>public</b> members of 
  2738. a class first in one 
  2739. group and then list all 
  2740. the <b>private</b> members in 
  2741. another group. This 
  2742. focuses the client's <br>
  2743.  
  2744. </page>
  2745. <page>
  2746. attention on the class's 
  2747. <b>public</b> interface, rather 
  2748. than on the class's 
  2749. implementation.<br>
  2750. <br>
  2751.  
  2752. </page>
  2753. <page>
  2754. If you choose to list the 
  2755. <b>private</b> members first 
  2756. in a class definition, 
  2757. explicitly use the 
  2758. <b>private</b> label despite 
  2759. the fact that <b>private</b> is 
  2760. assumed by default. 
  2761. This improves program 
  2762. clarity. Our preference 
  2763. is to list the <b>public</b> <br>
  2764.  
  2765. </page>
  2766. <page>
  2767. members of a class first 
  2768. to emphasize the class's 
  2769. interface.<br>
  2770. <br>
  2771.  
  2772. </page>
  2773. <page>
  2774. When appropriate 
  2775. (almost always), 
  2776. provide a constructor to 
  2777. ensure that every object 
  2778. is properly initialized 
  2779. with meaningful 
  2780. values. Pointer data 
  2781. members, in particular, 
  2782. should be initialized to <br>
  2783.  
  2784. </page>
  2785. <page>
  2786. some legitimate pointer 
  2787. value or to 0.<br>
  2788. <br>
  2789.  
  2790. </page>
  2791. <page>
  2792. Declare default 
  2793. function argument 
  2794. values only in the 
  2795. function prototype 
  2796. within the class 
  2797. definition in the header 
  2798. file.<br>
  2799. <br>
  2800.  
  2801. </page>
  2802. <page>
  2803. Member functions that 
  2804. set the values of private 
  2805. data should verify that 
  2806. the intended new values 
  2807. are proper; if they are 
  2808. not, the set functions 
  2809. should place the 
  2810. <b>private</b> data members 
  2811. into an appropriate 
  2812. consistent state.<br>
  2813.  
  2814. </page>
  2815. <page>
  2816. Never have a <b>public</b> 
  2817. member function return 
  2818. a non-<b>const</b> reference 
  2819. (or a pointer) to a 
  2820. <b>private</b> data member. 
  2821. Returning such a 
  2822. reference violates the 
  2823. encapsulation of the 
  2824. class.<br>
  2825. <br>
  2826.  
  2827. </page>
  2828. </section>
  2829. <section type=Popup name=Errors title="Common Errors">
  2830. <page>
  2831. The expression 
  2832. <b>(*timePtr).hour </b>refers 
  2833. to the <b>hour</b> member of 
  2834. the <b>struct</b> pointed to by 
  2835. <b>timePtr</b>. Omitting the 
  2836. parentheses as in 
  2837. <b>*timePtr.hour </b>would 
  2838. be a syntax error 
  2839. because <b>.</b> has a higher 
  2840. precedence than <tt><i>*<b>  </b></i></tt>so <br>
  2841.  
  2842. </page>
  2843. <page>
  2844. the expression would 
  2845. execute as if 
  2846. parenthesized as 
  2847. <b>*(timePtr.hour). </b>This 
  2848. would be a syntax error 
  2849. because with a pointer 
  2850. you must use the arrow 
  2851. operator to refer to a 
  2852. member<br>
  2853. <br>
  2854.  
  2855. </page>
  2856. <page>
  2857. Forgetting the 
  2858. semicolon at the end of 
  2859. a class (or structure) 
  2860. definition is a syntax 
  2861. error.<br>
  2862. <br>
  2863.  
  2864. </page>
  2865. <page>
  2866. Specifying a return type 
  2867. and/or a return value 
  2868. for a constructor is a 
  2869. syntax error.<br>
  2870. <br>
  2871.  
  2872. </page>
  2873. <page>
  2874. Attempting to initialize 
  2875. a data member of a 
  2876. class explicitly in the 
  2877. class definition is a 
  2878. syntax error. <br>
  2879. <br>
  2880.  
  2881. </page>
  2882. <page>
  2883. When defining a class's 
  2884. member functions 
  2885. outside that class, 
  2886. omitting the class name 
  2887. and scope resolution 
  2888. operator on the function 
  2889. name is a syntax error.<br>
  2890. <br>
  2891.  
  2892. </page>
  2893. <page>
  2894. An attempt by a 
  2895. function which is not a 
  2896. member of a particular 
  2897. class (or a <b>friend</b> of 
  2898. that class) to access a 
  2899. <b>private</b> member of that 
  2900. class is a syntax error.<br>
  2901. <br>
  2902.  
  2903. </page>
  2904. <page>
  2905. Data members of a 
  2906. class cannot be 
  2907. initialized in the class 
  2908. definition. <br>
  2909. <br>
  2910.  
  2911. </page>
  2912. <page>
  2913. Attempting to declare a 
  2914. return type for a 
  2915. constructor and/or 
  2916. attempting to return a 
  2917. value from a 
  2918. constructor are syntax 
  2919. errors.<br>
  2920. <br>
  2921.  
  2922. </page>
  2923. <page>
  2924. Specifying default 
  2925. initializers for the same 
  2926. member function in 
  2927. both a header file and in 
  2928. the member function 
  2929. definition.<br>
  2930. <br>
  2931.  
  2932. </page>
  2933. <page>
  2934. It is a syntax error to 
  2935. attempt to pass 
  2936. arguments to a 
  2937. destructor, to specify a 
  2938. return type for a 
  2939. destructor (even <b>void</b> 
  2940. cannot be specified), to 
  2941. return values from a 
  2942. destructor, or to 
  2943. overload a destructor.<br>
  2944.  
  2945. </page>
  2946. <page>
  2947. A constructor can call 
  2948. other member functions 
  2949. of the class such as set 
  2950. or get functions, but 
  2951. because the constructor 
  2952. is initializing the 
  2953. object, the data 
  2954. members may not yet 
  2955. be in a consistent state. 
  2956. Using data members <br>
  2957.  
  2958. </page>
  2959. <page>
  2960. before they have been 
  2961. properly initialized can 
  2962. cause logic errors.<br>
  2963. <br>
  2964.  
  2965. </page>
  2966. </section>
  2967. <section type=Popup name=Engineer title="Engineering">
  2968. <page>
  2969. To avoid the overhead 
  2970. of call-by-value yet still 
  2971. gain the benefit that the 
  2972. caller's original data is 
  2973. protected from 
  2974. modification, pass 
  2975. large-size arguments as 
  2976. <b>const</b> references.<br>
  2977. <br>
  2978.  
  2979. </page>
  2980. <page>
  2981. It is important to write 
  2982. programs that are 
  2983. understandable and 
  2984. easy to maintain. 
  2985. Change is the rule 
  2986. rather than the 
  2987. exception. 
  2988. Programmers should 
  2989. anticipate that their 
  2990. code will be modified. <br>
  2991.  
  2992. </page>
  2993. <page>
  2994. As we will see, classes 
  2995. can facilitate program 
  2996. modifiability.<br>
  2997. <br>
  2998.  
  2999. </page>
  3000. <page>
  3001. Clients of a class use 
  3002. the class without 
  3003. knowing the internal 
  3004. details of how the class 
  3005. is implemented. If the 
  3006. class implementation is 
  3007. changed (to improve 
  3008. performance, for 
  3009. example), provided the 
  3010. class's interface <br>
  3011.  
  3012. </page>
  3013. <page>
  3014. remains constant, the 
  3015. class's client source 
  3016. code need not change 
  3017. (although the client 
  3018. may need to be 
  3019. recompiled). This 
  3020. makes it much easier to 
  3021. modify systems. <br>
  3022. <br>
  3023.  
  3024. </page>
  3025. <page>
  3026. Member functions are 
  3027. usually shorter than 
  3028. functions in non-object-
  3029. oriented programs 
  3030. because the data stored 
  3031. in data members has 
  3032. ideally been validated 
  3033. by a constructor and/or 
  3034. by member functions 
  3035. that store new data. <br>
  3036.  
  3037. </page>
  3038. <page>
  3039. Because the data is 
  3040. already the object, the 
  3041. member function calls 
  3042. often have no 
  3043. arguments or at least 
  3044. have fewer arguments 
  3045. than typical function 
  3046. calls in non-object-
  3047. oriented languages. 
  3048. Thus, the calls are <br>
  3049.  
  3050. </page>
  3051. <page>
  3052. shorter, the function 
  3053. definitions are shorter 
  3054. and the function 
  3055. prototypes are shorter.<br>
  3056. <br>
  3057.  
  3058. </page>
  3059. <page>
  3060. Clients have access to a 
  3061. class's interface but 
  3062. should not have access 
  3063. to a class's 
  3064. implementation.<br>
  3065. <br>
  3066.  
  3067. </page>
  3068. <page>
  3069. Declaring member 
  3070. functions inside a class 
  3071. definition (via their 
  3072. function prototypes) 
  3073. and defining those 
  3074. member functions 
  3075. outside that class 
  3076. definition separates the 
  3077. interface of a class from 
  3078. its implementation. <br>
  3079.  
  3080. </page>
  3081. <page>
  3082. This promotes good 
  3083. software engineering. 
  3084. Clients of a class 
  3085. cannot see the 
  3086. implementation of that 
  3087. class's member 
  3088. functions.<br>
  3089. <br>
  3090.  
  3091. </page>
  3092. <page>
  3093. Only the simplest 
  3094. member functions 
  3095. should be defined in the 
  3096. class header.<br>
  3097. <br>
  3098.  
  3099. </page>
  3100. <page>
  3101. Using an object-
  3102. oriented programming 
  3103. approach can often 
  3104. simplify function calls 
  3105. by reducing the number 
  3106. of parameters to be 
  3107. passed. This benefit of 
  3108. object-oriented 
  3109. programming derives 
  3110. from the fact that <br>
  3111.  
  3112. </page>
  3113. <page>
  3114. encapsulation of data 
  3115. members and member 
  3116. functions within an 
  3117. object gives the 
  3118. member functions the 
  3119. right to access the data 
  3120. members.<br>
  3121. <br>
  3122.  
  3123. </page>
  3124. <page>
  3125. A central theme of this 
  3126. book is "reuse, reuse, 
  3127. reuse." We will 
  3128. carefully discuss a 
  3129. number of techniques 
  3130. for "polishing" classes 
  3131. to encourage reuse. We 
  3132. focus on "crafting 
  3133. valuable classes" and <br>
  3134.  
  3135. </page>
  3136. <page>
  3137. creating valuable 
  3138. "software assets."<br>
  3139. <br>
  3140.  
  3141. </page>
  3142. <page>
  3143. Clients of a class do not 
  3144. need access to the 
  3145. class's source code in 
  3146. order to use the class. 
  3147. The clients do, 
  3148. however, need to be 
  3149. able to link to the 
  3150. class's object code. 
  3151. This encourages 
  3152. independent software <br>
  3153.  
  3154. </page>
  3155. <page>
  3156. vendors (ISVs) to 
  3157. provide class libraries 
  3158. for sale or license. The 
  3159. ISVs provide in their 
  3160. products only the 
  3161. header files and the 
  3162. object modules. No 
  3163. proprietary information 
  3164. is revealed--as would 
  3165. be the case if source <br>
  3166.  
  3167. </page>
  3168. <page>
  3169. code were provided. 
  3170. The C++ user 
  3171. community benefits by 
  3172. having more ISV-
  3173. produced class libraries 
  3174. available.<br>
  3175. <br>
  3176.  
  3177. </page>
  3178. <page>
  3179. Place the class 
  3180. declaration in a header 
  3181. file to be included by 
  3182. any client that wants to 
  3183. use the class. This 
  3184. forms the class's <b>public</b> 
  3185. interface (and provides 
  3186. the client with the 
  3187. function prototypes it 
  3188. needs to be able to call <br>
  3189.  
  3190. </page>
  3191. <page>
  3192. the class's member 
  3193. functions). Place the 
  3194. definitions of the class 
  3195. member functions in a 
  3196. source file. This forms 
  3197. the implementation of 
  3198. the class.<br>
  3199. <br>
  3200.  
  3201. </page>
  3202. <page>
  3203. Information important 
  3204. to the interface to a 
  3205. class should be 
  3206. included in the header 
  3207. file. Information that 
  3208. will be used only 
  3209. internally in the class 
  3210. and will not be needed 
  3211. by clients of the class 
  3212. should be included in <br>
  3213.  
  3214. </page>
  3215. <page>
  3216. the unpublished source 
  3217. file. This is yet another 
  3218. example of the 
  3219. principle of least 
  3220. privilege.<br>
  3221. <br>
  3222.  
  3223. </page>
  3224. <page>
  3225. C++ encourages 
  3226. programs to be 
  3227. implementation 
  3228. independent. When the 
  3229. implementation of a 
  3230. class used by 
  3231. implementation-
  3232. independent code 
  3233. changes, that code need 
  3234. not be modified, but it <br>
  3235.  
  3236. </page>
  3237. <page>
  3238. may need to be 
  3239. recompiled.<br>
  3240. <br>
  3241.  
  3242. </page>
  3243. <page>
  3244. Keep all the data 
  3245. members of a class 
  3246. <b>private</b>. Provide <b>public</b> 
  3247. member functions to set 
  3248. the values of <b>private</b> 
  3249. data members and to 
  3250. get the values of 
  3251. <b>private</b> data members. 
  3252. This architecture helps 
  3253. hide the <br>
  3254.  
  3255. </page>
  3256. <page>
  3257. implementation of a 
  3258. class from its clients, 
  3259. which reduces bugs and 
  3260. improves program 
  3261. modifiability.<br>
  3262. <br>
  3263.  
  3264. </page>
  3265. <page>
  3266. Class designers use 
  3267. <b>private</b>, <b>protected</b>, and 
  3268. <b>public</b> members to 
  3269. enforce the notion of 
  3270. information hiding and 
  3271. the principle of least 
  3272. privilege.<br>
  3273. <br>
  3274.  
  3275. </page>
  3276. <page>
  3277. The class designer need 
  3278. not provide set and/or 
  3279. get functions for each 
  3280. <b>private</b> data item; these 
  3281. capabilities should be 
  3282. provided only when 
  3283. appropriate.<br>
  3284. <br>
  3285.  
  3286. </page>
  3287. <page>
  3288. Member functions tend 
  3289. to fall into a number of 
  3290. different categories: 
  3291. functions that read and 
  3292. return the value of 
  3293. private data members; 
  3294. functions that set the 
  3295. value of private data 
  3296. members; functions 
  3297. that implement the <br>
  3298.  
  3299. </page>
  3300. <page>
  3301. features of the class; 
  3302. and functions that 
  3303. perform various 
  3304. mechanical chores for 
  3305. the class such as 
  3306. initializing class 
  3307. objects, assigning class 
  3308. objects, converting 
  3309. between classes and 
  3310. built-in types or <br>
  3311.  
  3312. </page>
  3313. <page>
  3314. between classes and 
  3315. other classes, and 
  3316. handling memory for 
  3317. class objects.<br>
  3318. <br>
  3319.  
  3320. </page>
  3321. <page>
  3322. A phenomenon of 
  3323. object-oriented 
  3324. programming is that 
  3325. once a class is defined, 
  3326. creating and 
  3327. manipulating objects of 
  3328. that class usually 
  3329. involves issuing only a 
  3330. simple sequence of 
  3331. member function <br>
  3332.  
  3333. </page>
  3334. <page>
  3335. calls--few, if any, 
  3336. control structures are 
  3337. needed. By contrast, it 
  3338. is common to have 
  3339. control structures in the 
  3340. implementation of a 
  3341. class's member 
  3342. functions.<br>
  3343. <br>
  3344.  
  3345. </page>
  3346. <page>
  3347. If a member function of 
  3348. a class already provides 
  3349. all or part of the 
  3350. functionality required 
  3351. by a constructor (or 
  3352. other member function) 
  3353. of the class, call that 
  3354. member function from 
  3355. the constructor (or 
  3356. other member <br>
  3357.  
  3358. </page>
  3359. <page>
  3360. function). This 
  3361. simplifies the 
  3362. maintenance of the 
  3363. code and reduces the 
  3364. likelihood of an error if 
  3365. the implementation of 
  3366. the code is modified. 
  3367. As a general rule: 
  3368. Avoid repeating code.<br>
  3369. <br>
  3370.  
  3371. </page>
  3372. <page>
  3373. It is possible for a class 
  3374. not to have a default 
  3375. constructor.<br>
  3376. <br>
  3377.  
  3378. </page>
  3379. <page>
  3380. As we will see 
  3381. (throughout the 
  3382. remainder of the book), 
  3383. constructors and 
  3384. destructors have much 
  3385. greater prominence in 
  3386. C++ and object-
  3387. oriented programming 
  3388. than is possible to <br>
  3389.  
  3390. </page>
  3391. <page>
  3392. convey after only our 
  3393. brief introduction here.<br>
  3394. <br>
  3395.  
  3396. </page>
  3397. <page>
  3398. Making data members 
  3399. <b>private</b> and controlling 
  3400. access, especially write 
  3401. access, to those data 
  3402. members through 
  3403. <b>public</b> member 
  3404. functions helps ensure 
  3405. data integrity.<br>
  3406. <br>
  3407.  
  3408. </page>
  3409. <page>
  3410. Accessing <b>private</b> data 
  3411. through set and get 
  3412. member functions not 
  3413. only protects the data 
  3414. members from 
  3415. receiving invalid 
  3416. values, but it also 
  3417. insulates clients of the 
  3418. class from the 
  3419. representation of the <br>
  3420.  
  3421. </page>
  3422. <page>
  3423. data members. Thus, if 
  3424. the representation of 
  3425. the data changes for 
  3426. some reason (typically 
  3427. to reduce the amount of 
  3428. storage required or to 
  3429. improve performance), 
  3430. only the member 
  3431. functions need to 
  3432. change--the clients <br>
  3433.  
  3434. </page>
  3435. <page>
  3436. need not change as long 
  3437. as the interface 
  3438. provided by the 
  3439. member functions 
  3440. remains the same. The 
  3441. clients may, however, 
  3442. need to be recompiled.<br>
  3443. <br>
  3444.  
  3445. </page>
  3446. </section>
  3447.  
  3448. <section type=Popup name=Portable title="Portability">
  3449. <page>
  3450. This chapter does not contain any Portability tips.
  3451. </page>
  3452. </section>
  3453. <section type=Popup name=AppletPopup title="Applet Examples">
  3454. <page>
  3455. This chapter does not contain any Applet Examples.
  3456. </page>
  3457. </section>
  3458. </chapter>
  3459. </html>
  3460. </html>
  3461.